What is the difference between rw+ and r+

12,683

On Linux at least, there's no difference as far as I can tell. Here's a test script

f1 = open('f1', 'r+')
f2 = open('f2', 'rw+')
f3 = open('f3', 'w+')

and its corresponding OS syscalls (using strace); tested on python 2.7.9.

open("f1", O_RDWR|O_LARGEFILE)          = 3
open("f2", O_RDWR|O_LARGEFILE)          = 4
open("f3", O_RDWR|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = 5

See http://man7.org/linux/man-pages/man2/open.2.html for more details on the file access and creation flags.

It's not accurate to say a file object opened with 'r+' cannot be used to truncate a file - it just doesn't do so at the point the file is opened.

Share:
12,683
Moon Cheesez
Author by

Moon Cheesez

Hi there, I use StackExchange to solve my problems and sometimes help others the best I can. I also use StackExchange for memes like: "Don't see bug, no free-hand red circle." — rene "Unless you want to represent the national debt, in which case max_digits has to be > 20" – Bron Davies "On a typical day, how much time do you spend outside?" - StackOverflow Developer Survey (can't remember which year) (Good PHP Idioms) "Declare Variables before using them!" - Answer by Kevin Thou shalt not create entities with more than one component of the same type or else thou shalt face eternal damnation. - Dragon Energy

Updated on June 08, 2022

Comments

  • Moon Cheesez
    Moon Cheesez almost 2 years

    I stumbled onto this stackoverflow question while doing some file IO: Confused by python file mode "w+"

    • r for reading
    • w for writing
    • r+ opens for reading and writing (cannot truncate a file)
    • w+ for writing and reading (can truncate a file)
    • rb+ reading or writing a binary file
    • wb+ writing a binary file
    • a+ opens for appending

    Note the r+ cannot truncate a file. So I was looking for something that could truncate the file after reading it which led me to another SO link: Python truncate lines as they are read

    I saw that they used a different mode, rw+, which was not documented. From how it was used in the answer, I guessed it meant "open for reading, writing and truncating but do not truncate on open".

    I later tested this mode and it seems that it was removed in Python 3, hence throwing a ValueError when used:

    Python 2:

    f = open("myfile.txt", "rw+")
    text = f.read()
    f.truncate(0)
    f.close()
    

    Python 3:

    f = open("myfile.txt", "rw+")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: must have exactly one of create/read/write/append mode
    

    However, I needed a file mode in Python 3 that can truncate as well as read but not truncate on open. So, after a bit more testing, I found that r+ can actually truncate in both Python 2 and 3.

    Python 2:

    f = open("myfile.txt", "r+")
    text = f.read()
    f.truncate(0)
    f.seek(0, 0)
    print f.read()
    f.close()
    

    Will print out nothing.

    Python 3:

    f = open("myfile.txt", "r+")
    text = f.read()
    f.truncate(0)
    f.seek(0, 0)
    print(f.read())
    f.close()
    

    Will also print out nothing.

    My question is, if both r+ and rw+ can truncate, what are the differences between them in Python 2?