[Tutor] Simultaneous read and write on file
Martin A. Brown
martin at linux-ip.net
Mon Jan 18 23:04:38 EST 2016
Hello,
>> I have read in documentation that wb+ mode is for writing and
>> reading. Am i using wrong mode, should i use rb+ ?
>
>Use w+ to create a new file, opened with read and write access. Use
>r+ to open an existing file with read and write access. Unlike w+,
>r+ does not truncate the file and will not create a new file.
[snip]
>FYI, in terms of POSIX open flags [1], the file mode gets mapped as follows:
>
> | flags | no + | +
> -----------------------------------------
> r | | O_RDONLY | O_RDWR
> w | O_CREAT, O_TRUNC | O_WRONLY | O_RDWR
> a | O_CREAT, O_APPEND | O_WRONLY | O_RDWR
>
>Python 3 only:
>
> x | O_CREAT, O_EXCL | O_WRONLY | O_RDWR
>
>In terms of Windows open dispositions and access modes [2], the file
>mode gets mapped as follows:
>
> | disposition | no + | +
> ---------------------------------------------------------------
> r | OPEN_EXISTING | GENERIC_READ | GENERIC_READ, GENERIC_WRITE
> w | CREATE_ALWAYS | GENERIC_WRITE | GENERIC_READ, GENERIC_WRITE
> a | OPEN_ALWAYS | GENERIC_WRITE | GENERIC_READ, GENERIC_WRITE
>
>Python 3 only:
>
> x | CREATE_NEW | GENERIC_WRITE | GENERIC_READ, GENERIC_WRITE
The above is a very handy chart. Did you find this somewhere, eryk
sun, or is this from your own knowledge and experience? This might
benefit an experienced Windows/POSIX user trying to understand
open() in Python if it were available in the standard documentation.
Thanks for mapping this to common operating systems. I had inferred
this already, but this is a great summary.
-Martin
--
Martin A. Brown
http://linux-ip.net/
More information about the Tutor
mailing list