I have recently been going through some of our windows python 2.4 code and come across this:
self.logfile = open(self.logfile_name, "wua")
I know what w
, u
and a
do on their own, but what happens when you combine them?
The a
is superfluous. wua
is the same as wu
since w
comes first and will thus truncate the file. If you would reverse the order, that is, auw
, that would be the same as au
. Visualized:
>>> f = open('test.txt', 'r')
>>> f.read()
'Initial contents\n'
>>> f.close()
>>> f = open('test.txt', 'wua')
>>> print >> f, 'writing'
>>> f.close()
>>> f = open('test.txt', 'r')
>>> f.read()
'writing\n'
>>> f.close()
>>> f = open('test.txt', 'auw')
>>> print >> f, 'appending'
>>> f.close()
>>> f = open('test.txt', 'r')
>>> f.read()
'writing\nappending\n'
>>> f.close()
(Reminder: both a
and w
open the file for writing, but the former appends while the other truncates.)
I did not notice that you knew what the modifiers did. Combined they will do the following:
A and W together is superfluous since both will open for writing. When using W, the file will be overwritten. When using A, all new text is appended after the existing content.
U means "open file XXX for input as a text file with universal newline interpretation".
More here: http://codesnippets.joyent.com/posts/show/1969
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With