Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7913693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:54:16+00:00 2026-06-03T13:54:16+00:00

I want to: open a file in read-write mode if it exists; create it

  • 0

I want to:

  • open a file in read-write mode if it exists;
  • create it if it doesn’t exist;
  • be able to truncate it anytime-anywhere.

EDIT: with truncate I mean write until a position and discard the remaining part of the file, if present

All this atomically (with a single open() call or simulating a single open() call)

No single open modality seems to apply:

  • r : obviously doesn’t work;
  • r+ : fails if the file doesn’t exist;
  • w: recreate the file if it exists;
  • w+: recreate the file if it exists;
  • a: can’t read;
  • a+: can’t truncate.

Some combinations I tried (rw, rw+, r+w, etc.) seems to not work either. Is it possible?

Some doc from Ruby (applies to python too):

r
Read-only mode. The file pointer is placed at the beginning of the file.
This is the default mode.

r+
Read-write mode. The file pointer will be at the beginning of the file.

w
Write-only mode. Overwrites the file if the file exists. If the file
does not exist, creates a new file for writing.

w+
Read-write mode. Overwrites the existing file if the file exists. If the
file does not exist, creates a new file for reading and writing.

a
Write-only mode. The file pointer is at the end of the file if the file
exists. That is, the file is in the append mode. If the file does not exist,
it creates a new file for writing.

a+
Read and write mode. The file pointer is at the end of the file if the file
exists. The file opens in the append mode. If the file does not exist, it
creates a new file for reading and writing.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:54:18+00:00Added an answer on June 3, 2026 at 1:54 pm

    According to OpenGroup:

    O_TRUNC

    If the file exists and is a regular file, and the file is successfully
    opened O_RDWR or O_WRONLY, its length is truncated to 0 and the mode
    and owner are unchanged. It will have no effect on FIFO special files
    or terminal device files. Its effect on other file types is
    implementation-dependent. The result of using O_TRUNC with O_RDONLY is
    undefined.

    So, O_TRUNC is probably passed when opening a file with “w” or “w+”. This gives “truncation” a different meaning, not what I want.

    With python the solution seems to open file at low-level I/O with os.open() function.

    The following python function:

    def touchopen(filename, *args, **kwargs):
        # Open the file in R/W and create if it doesn't exist. *Don't* pass O_TRUNC
        fd = os.open(filename, os.O_RDWR | os.O_CREAT)
    
        # Encapsulate the low-level file descriptor in a python file object
        return os.fdopen(fd, *args, **kwargs)
    

    has the behavior I wanted. You can use it like this (it’s in fact my use case):

    # Open an existing file or create if it doesn't exist
    with touchopen("./tool.run", "r+") as doing_fd:
    
        # Acquire a non-blocking exclusive lock
        fcntl.lockf(doing_fd, fcntl.LOCK_EX)
    
        # Read a previous value if present
        previous_value = doing_fd.read()
        print previous_value 
    
        # Write the new value and truncate
        doing_fd.seek(0)
        doing_fd.write("new value")
        doing_fd.truncate()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to read and write a large file in parallel,
I want to open file and read it that I pass from console. Like
What I want to do is overwrite a read-only file. With a read/write file.
I want to read a file and write it back out. Here's my code:
I want to open a file for reading, the C++ way. I need to
I want to open a file dialog box in user control. I used using
I want to open a file from a Class in C# using a Process,
I want to open a file for reading. However, in the context of this
I want to open pdf file in webview but the scenario is different here,
Suppose I want to open a file in an existing Emacs session using su

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.