Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python file modes detail

Tags:

python

file-io

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?

like image 294
Xolve Avatar asked Mar 17 '09 14:03

Xolve


People also ask

What are the different file modes in Python?

Python file modes. Don’t confuse, read about very mode as below. r for reading – The file pointer is placed at the beginning of the file. This is the default mode. r+ Opens a file for both reading and writing. The file pointer will be at the beginning of the file. w Opens a file for writing only. Overwrites the file if the file exists.

What is a mode in Python?

a Mode in Python File Opening The a mode opens the file for the purpose of appending. The file pointer in this mode is placed at the end of the file if it already exists in the system. If the file does not exist, then it is created for writing.

What are the modes of Reading and writing in Python?

Python file modes. Don’t confuse, read about very mode as below. r for reading – The file pointer is placed at the beginning of the file. This is the default mode. r+ Opens a file for both reading and writing. The file pointer will be at the beginning of the file. w Opens a file for writing only.

How to open a file in read mode in Python?

We use open () function in Python to open a file in read or write mode. As explained above, open () will return a file object. To return a file object we use open () function along with two arguments, that accepts file name and the mode, whether to read or write. So, the syntax being: open (filename, mode).


2 Answers

Better yet, let the documentation do it for you: http://docs.python.org/library/functions.html#open. Your issue in the question is that there is no "rw" mode... you probably want 'r+' as you wrote (or 'a+' if the file does not yet exist).

like image 162
Jarret Hardie Avatar answered Nov 15 '22 22:11

Jarret Hardie


As an addition to @Jarret Hardie's answer here's how Python check file mode in the function fileio_init():

s = mode;
while (*s) {
    switch (*s++) {
    case 'r':
        if (rwa) {
        bad_mode:
            PyErr_SetString(PyExc_ValueError,
                    "Must have exactly one of read/write/append mode");
            goto error;
        }
        rwa = 1;
        self->readable = 1;
        break;
    case 'w':
        if (rwa)
            goto bad_mode;
        rwa = 1;
        self->writable = 1;
        flags |= O_CREAT | O_TRUNC;
        break;
    case 'a':
        if (rwa)
            goto bad_mode;
        rwa = 1;
        self->writable = 1;
        flags |= O_CREAT;
        append = 1;
        break;
    case 'b':
        break;
    case '+':
        if (plus)
            goto bad_mode;
        self->readable = self->writable = 1;
        plus = 1;
        break;
    default:
        PyErr_Format(PyExc_ValueError,
                 "invalid mode: %.200s", mode);
        goto error;
    }
}

if (!rwa)
    goto bad_mode;

That is: only "rwab+" characters are allowed; there must be exactly one of "rwa", at most one '+' and 'b' is a noop.

like image 33
jfs Avatar answered Nov 15 '22 22:11

jfs