What is the difference between rw+ and r+

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.

Leave a Comment