In Python, the following statements do not work:
f = open("ftmp", "rw") print >> f, "python"
I get the error:
Traceback (most recent call last): File "<stdin>", line 1, in <module> IOError: [Errno 9] Bad file descriptor
But with the following code it works:
g = open("ftmp", "r+") print >> g, "python"
It looks like I need to revise the file modes. What are the deep intricacies of the file opening modes?
Update: Python 3, doesn’t allow "rw" mode. You would get the error:
ValueError: must have exactly one of create/read/write/append mode
As an addition to @Jarret Hardie’s answer here’s how Python check file mode in the function fileio_init():
That is: only
'rwab+'characters are allowed; there must be exactly one of'rwa', at most one'+'and'b'is a noop.