Lesson 17 Reading and Writing Files Python Mini-Course University of Oklahoma Department of Psychology 1 Python Mini-Course: Lesson 17 5/10/09 Lesson objectives 1. Open files for reading, writing, or appending data 2. Write data to a text file 3. Use the os module to manipulate paths and pathnames 4. Use the pickle module to store complex data types 2 Python Mini-Course: Lesson 17 5/10/09 Files in Python Files are objects http://www.python.org/doc/2.5.2/lib /bltin-file-objects.html Python file methods are wrappers for the standard C stdio package 3 Python Mini-Course: Lesson 17 5/10/09 File types Text file Contains ACSII or Unicode characters Can be created and read by most applications Text editors (Notepad, SimpleText, etc.) IDEs (IDLE, SPE, Eclipse, etc.) Word processors (MS Word, etc.) Spreadsheet programs (Excel, etc.) Other apps (SAS, SPSS, R, Mathmatica, etc.) 4 Python Mini-Course: Lesson 17 5/10/09 File types Binary file Contain data coded in other formats Examples: JPEG images Audio or video clips Packed binary data from FORTRAN Matlab data files (.m files) 5 Python Mini-Course: Lesson 17 5/10/09 The open statement Returns a file object for access with file methods Syntax fid = open(filename, mode) where fid is the name of the file object 6 Python Mini-Course: Lesson 17 5/10/09 The filename argument Should be a string containing the complete name of the file, including the file extension NB: In MS Windows, most file extensions are hidden in Windows Explorer Can include a partial or complete path Default path is the folder containing the main script (.py file) 7 Python Mini-Course: Lesson 17 5/10/09 File modes: reading a file 'r' 'rb' read (text file) read (binary file) Can read file contents but cannot change file If file does not exist, raises exception 8 Python Mini-Course: Lesson 17 5/10/09 File modes: writing 'w' 'wb' write (text file) write (binary file) Create a new file Overwrites existing file if there is one 9 Python Mini-Course: Lesson 17 5/10/09 File modes: append 'a' 'ab' append (text file) append (binary file) Append data to (the end of) a file If file does not exist, creates a new file 10 Python Mini-Course: Lesson 17 5/10/09 File modes: mixed modes 'r+' read and write existing file If file does not exist, raises exception 'a+' read and write existing file Creates new file if one does not exist 'w+' read and write a new file Overwrites file if it already exists 11 Python Mini-Course: Lesson 17 5/10/09 Note Data transferred between files and your programs is represented as Python strings, even if it is binary data. String objects can contain character bytes of any value 12 Python Mini-Course: Lesson 17 5/10/09 End-of-line translations Unix and Linux (and Mac OS X) Use newline: \n DOS and Windows Use return + newline: \r\n Old Mac OSs Use return: \r 13 Python Mini-Course: Lesson 17 5/10/09 End-of-line translations Python automatically translates Windows EOLs when reading and writing files on Windows platforms When in text mode Not in binary mode 14 Python Mini-Course: Lesson 17 5/10/09 Example: eol.py, win.txt, mac.txt text_mode = [open('win.txt','r').read(), open('mac.txt','r').read()] print 'Text mode:' print text_mode binary_mode = [open('win.txt','rb').read(), open('mac.txt','rb').read()] print '\nBinary mode:' print binary_mode 15 Python Mini-Course: Lesson 17 5/10/09 File read methods file.read() Read all data until EOF is reached and return as a string object file.readline() Read one entire line from the file (keeps the trailing newline character) and return as a string object file.readlines() Read until EOF using readline() and return a list containing the lines thus read 16 Python Mini-Course: Lesson 17 5/10/09 Example: read.py fin = open('win.txt', 'r') print fin.read() fin.seek(0) print fin.readline() fin.seek(0) print fin.readlines() fin.close() 17 Python Mini-Course: Lesson 17 5/10/09 File write methods file.write(str) Write a string to the file NB: Due to buffering, the string may not actually show up in the file until the flush() or close() method is called file.writelines(sequence) Write a sequence of strings to the file NB: Does not add line separators, but this can be done using the string join operator 18 Python Mini-Course: Lesson 17 5/10/09 Example: randnums.py import random fout = open('rand.txt', 'w') fout.write('Number\n') seq = [] for i in range(10): s = '%2.4f' % (random.random()) seq.append(s) fout.writelines('\n'.join(seq)) fout.close() 19 Python Mini-Course: Lesson 17 5/10/09 Example: randnums2.py import random fout = open('rand.txt', 'w') fout.write('Index\tNumber\n') seq = [] for i in range(10): s = '%d\t%2.4f' % (i, random.random()) seq.append(s) fout.write('\n'.join(seq)) fout.close() 20 Python Mini-Course: Lesson 17 5/10/09 The os module Provides generic operating system (OS) support and a standard, platform-independent OS interface Includes tools for environments, processes, files, shell commands, and much more http://www.python.org/doc/2.5.4/lib/ module-os.html 21 Python Mini-Course: Lesson 17 5/10/09 File and directory commands os.getcwd() Returns the name of the current wording directory as a string os.chdir(path) Changes the current working directory for this process to path, a directory name string 22 Python Mini-Course: Lesson 17 5/10/09 File and directory commands os.listdir(path) Returns a list of names of all the entries in the directory path 23 Python Mini-Course: Lesson 17 5/10/09 Portability constants os.curdir() String for the current directory os.pardir() String for the parent directory os.sep() String used to separate directories os.linesep() String used to terminate lines 24 Python Mini-Course: Lesson 17 5/10/09 The pickle module Used to serialize and de-serialize a Python object structure http://www.python.org/doc/2.5.4 /lib/module-pickle.html 25 Python Mini-Course: Lesson 17 5/10/09 The pickle module Pickling the process whereby a Python object hierarchy is converted into a byte stream Unpickling the inverse operation, whereby a byte stream is converted back into an object hierarchy 26 Python Mini-Course: Lesson 17 5/10/09 The pickle module pickle.dump(obj, file) Write a pickled representation of obj to the open file object file pickle.load(file) Read a string from the open file object file and interpret it as a pickle data stream, reconstructing and returning the original object hierarchy 27 Python Mini-Course: Lesson 17 5/10/09 Example: pickling.py import random, pickle seq = [] for i in range(10): s = '%d\t%2.4f' % (i, random.random()) seq.append(s) print seq f = open('temp.pk', 'w') pickle.dump(seq, f) f.close() 28 Python Mini-Course: Lesson 17 5/10/09 Example: pickling.py seq = [] print seq f = open('temp.pk', 'r') seq = pickle.load(f) print seq 29 Python Mini-Course: Lesson 17 5/10/09