I have a socket that I am sending data to through a file created using the makefile method of a socket. However, the mode of the file created using makefile is 'wrb'.
I understand that 'w' = write, 'r' = read, and 'b' = binary. I also understand that you can combine them in a number of different ways, see Confused by python file mode "w+", which contains a list of possible combinations. However, I've never seen 'w' and 'r' together.
What is their behavior when together? For example, 'r+' allows reading and writing, and 'w+' does the same, except that it truncates the file beforehand. But what does 'wr' do?
The description in the Python 2.x docs suggests you would be able to both read and write to the file without closing it.
However, the behavior is not so.
Example:
f = open('myfile', 'wr')
f.write('THIS IS A TEST')
f.read()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 9] Bad file descriptor
It will write, however not read. If we open the file with the option reversed:
f = open('myfile', 'rw')
f.read()
f.write('THIS IS ALSO A TEST')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 9] Bad file descriptor
Observed behavior is that the open() function only takes the first character for file opening option, and disregards the rest except if it ends in a 'b', which would donate that it would be opened in binary mode.