Working with Files
An important part of programming is knowing how to work directly with files. Lets review the following Pythonic tools for working with files:
- readline() and readlines()
- the Pickle module
The open() function takes a filename and path as input and returns a file object.
f = open('/path/to/file', 'r')
The second parameter for the open() function is the file permission mode and is entered in the form of read (r), write (w), or append (a). If no mode parameter is passed, the mode defaults to read. To open binary files, append 'b' to the mode parameter, e.g. read binary ('rb').
The read() method will return the contents of the file as a string. Be careful that your computer has enough memory to read the entire file at once. read() accepts an optional parameter called 'size' that restricts Python to reading a specific portion of the file.
readline() and readlines()
The readline() and readlines() methods read one and multiple lines of a file respectively.
readline() returns one line of the file as a string with a new line character ( '\n' ) at the end. If readline() returns an empty string, the end of the file has been reached.
>>> f.readline() 'This is the first line.\n' >>> f.readline() 'This is the second line.\n' >>> f.readline() ' '
readlines() returns all of the lines in a file as a list. Each line in the list will have a newline character ( '\n' ) at the end.
>>> f.readlines() ['This is the first line\n', 'This is the second line\n']
The write() method takes a string as input and writes it to a file. Other data types must be converted to strings before they can be written to a file.
>>> data = 'This string will now be written to a file.' >>> f.write(data) 42
When you are done reading from or writing to a file you can close() the file and free memory.
After closing a file, attempts to access the file will fail.
f.readlines() Traceback (most recent call last): File "<pyshell#14>", line 1, in <module> f.readlines() ValueError: I/O operation on closed file.
The Pickle module enables developers to create persistent data objects. Any object or data in Python, even sometimes Python code, can be written to files using Pickle.
Pickled objects are converted to string representations and then unpickled later by converting them to their original types.
>>> import pickle >>> pickle_object = ['Some data to be pickled', 12345, ('apples', 'butter')] >>> pickle_file = open('pickle_file', 'wb') >>> pickle.dump(pickle_object, pickle_file) To retrieve pickled data, use pickle.load(). >>> pickled_data = open('pickled_file', 'rb') >>> de_pickled = pickle.load(pickled_data) >>> print(de_pickled) ['Some data to be pickled', 12345, ('apples', 'butter')]
Please complete the learning excercises in the Python for Informatics textbook. Post your code work to the web and a link to your files in this discussion.