Recipe2.2.Writing to a File


Recipe 2.2. Writing to a File

Credit: Luther Blissett

Problem

You want to write text or data to a file.

Solution

Here is the most convenient way to write one long string to a file:

open('thefile.txt', 'w').write(all_the_text)  # text to a text file open('abinfile', 'wb').write(all_the_data)    # data to a binary file

However, it is safer to bind the file object to a name, so that you can call close on the file object as soon as you're done. For example, for a text file:

file_object = open('thefile.txt', 'w') file_object.write(all_the_text) file_object.close( )

Often, the data you want to write is not in one big string, but in a list (or other sequence) of strings. In this case, you should use the writelines method (which, despite its name, is not limited to lines and works just as well with binary data as with text files!):

file_object.writelines(list_of_text_strings) open('abinfile', 'wb').writelines(list_of_data_strings)

Calling writelines is much faster than the alternatives of joining the strings into one big string (e.g., with ''.join) and then calling write, or calling write repeatedly in a loop.

Discussion

To create a file object for writing, you must always pass a second argument to open (or file)either 'w' to write textual data or 'wb' to write binary data. The same considerations detailed previously in Recipe 2.1 apply here, except that calling close explicitly is even more advisable when you're writing to a file rather than reading from it. Only by closing the file can you be reasonably sure that the data is actually on the disk and not still residing in some temporary buffer in memory.

Writing a file a little at a time is even more common than reading a file a little at a time. You can just call write and/or writelines repeatedly, as each string or sequence of strings to write becomes ready. Each write operation appends data at the end of the file, after all the previously written data. When you're done, call the close method on the file object. If all the data is available at once, a single writelines call is faster and simpler. However, if the data becomes available a little at a time, it's better to call write as the data comes, than to build up a temporary list of pieces (e.g., with append) just in order to be able to write it all at once in the end with writelines. Reading and writing are quite different, with respect to the performance and convenience implications of operating "in bulk" versus operating a little at a time.

When you open a file for writing with option 'w' (or 'wb'), any data that might already have been in the file is immediately destroyed; even if you close the file object immediately after opening it, you still end up with an empty file on the disk. If you want the data you're writing to be appended to the previous contents of the file, open the file with option 'a' (or 'ab') instead. More advanced options allow both reading and writing on the same open file objectin particular, see Recipe 2.8 for option 'r+b', which, in practice, is the only frequently used one out of all the advanced option strings.

See Also

Recipe 2.1; Recipe 2.8; documentation for the open built-in function and file objects in the Library Reference and Python in a Nutshell.



Python Cookbook
Python Cookbook
ISBN: 0596007973
EAN: 2147483647
Year: 2004
Pages: 420

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net