Upload
joy-stone
View
217
Download
0
Tags:
Embed Size (px)
Citation preview
Python Mini-CourseUniversity of Oklahoma
Department of Psychology
Lesson 17Reading and Writing Files
5/10/09Python Mini-Course: Lesson 171
Lesson objectives
1. Open files for reading, writing, or appending data
2. Write data to a text file3. Use the os module to manipulate
paths and pathnames4. Use the pickle module to store
complex data types
5/10/09Python Mini-Course: Lesson 172
Files in Python
Files are objectshttp://www.python.org/doc/2.5.2/lib/
bltin-file-objects.htmlPython file methods are wrappers for the standard C stdio package
5/10/09Python Mini-Course: Lesson 173
File types
Text fileContains ACSII or Unicode charactersCan be created and read by most
applicationsText 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.)
5/10/09Python Mini-Course: Lesson 174
File types
Binary fileContain data coded in other formatsExamples:
JPEG imagesAudio or video clipsPacked binary data from FORTRANMatlab data files (.m files)
5/10/09Python Mini-Course: Lesson 175
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
5/10/09Python Mini-Course: Lesson 176
The filename argument
Should be a string containing the complete name of the file, including the file extensionNB: In MS Windows, most file extensions
are hidden in Windows ExplorerCan include a partial or complete
pathDefault path is the folder containing the
main script (.py file)5/10/09Python Mini-Course: Lesson 177
File modes: reading a file
'r' read (text file)'rb' read (binary file)
Can read file contents but cannot change file
If file does not exist, raises exception
5/10/09Python Mini-Course: Lesson 178
File modes: writing
'w' write (text file)'wb' write (binary file)
Create a new fileOverwrites existing file if there is one
5/10/09Python Mini-Course: Lesson 179
File modes: append
'a' append (text file)'ab' append (binary file)
Append data to (the end of) a fileIf file does not exist, creates a new file
5/10/09Python Mini-Course: Lesson 1710
File modes: mixed modes
'r+' read and write existing fileIf file does not exist, raises exception
'a+' read and write existing fileCreates new file if one does not exist
'w+' read and write a new fileOverwrites file if it already exists
5/10/09Python Mini-Course: Lesson 1711
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
5/10/09Python Mini-Course: Lesson 1712
End-of-line translations
Unix and Linux (and Mac OS X)Use newline: \n
DOS and WindowsUse return + newline: \r\n
Old Mac OSsUse return: \r
5/10/09Python Mini-Course: Lesson 1713
End-of-line translations
Python automatically translates Windows EOLs when reading and writing files on Windows platformsWhen in text modeNot in binary mode
5/10/09Python Mini-Course: Lesson 1714
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
5/10/09Python Mini-Course: Lesson 1715
File read methods
file.read()Read all data until EOF is reached and return as
a string objectfile.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
5/10/09Python Mini-Course: Lesson 1716
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()
5/10/09Python Mini-Course: Lesson 1717
File write methods
file.write(str)Write a string to the fileNB: 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 fileNB: Does not add line separators, but this can be
done using the string join operator
5/10/09Python Mini-Course: Lesson 1718
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()
5/10/09Python Mini-Course: Lesson 1719
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()
5/10/09Python Mini-Course: Lesson 1720
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
5/10/09Python Mini-Course: Lesson 1721
File and directory commands
os.getcwd()Returns the name of the current wording
directory as a stringos.chdir(path)
Changes the current working directory for this process to path, a directory name string
5/10/09Python Mini-Course: Lesson 1722
File and directory commands
os.listdir(path)Returns a list of names of all the entries
in the directory path
5/10/09Python Mini-Course: Lesson 1723
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
5/10/09Python Mini-Course: Lesson 1724
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
5/10/09Python Mini-Course: Lesson 1725
The pickle module
Picklingthe process whereby a Python object hierarchy is converted into a byte stream
Unpicklingthe inverse operation, whereby a byte stream is converted back into an object hierarchy
5/10/09Python Mini-Course: Lesson 1726
The pickle module
pickle.dump(obj, file)Write a pickled representation of obj to
the open file object filepickle.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
5/10/09Python Mini-Course: Lesson 1727
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()
5/10/09Python Mini-Course: Lesson 1728