next up previous contents
Next: ``Printing'' to a File Up: File Objects Previous: Methods for Reading   Contents

Methods for Writing

Note that, in order to write to a file, the file must be opened with either the 'w' (write) or 'a' (append) modes. Furthermore, the objects to be written must be strings - no automatic conversion of of numbers to strings is carried out.

The write method takes a single scalar argument and writes it to the file represented by the file object on which it operates. Note that, unlike the print command, the write method does not automatically append a newline character to its argument. One consequence of this is that you can safely write binary files, or files with embedded null characters, using the write method. If you are writing a binary file, remember to append a 'b' to the second argument of the open function as described in Section 5.4.

The writelines method accepts a list argument, and writes each element of the list to the specified file as if a call to the write method had been made; no newlines are added to the output.


next up previous contents
Next: ``Printing'' to a File Up: File Objects Previous: Methods for Reading   Contents
Phil Spector 2003-11-12