Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Reading files in Python Girls’ Programming Network School of Information Technologies University of Sydney Mini-lecture 10 open reading Examples Summary TODO 2 Outline 1 opening files 2 Reading from a file 3 Examples 4 Summary 5 TODO Girls’ Programming Network Reading files Mini-lecture 10 open reading Examples Summary TODO 3 To read a file you need to open it first the open function creates an open file we need to pass in the name of the file ('test.txt') 1 2 3 4 >>> f = open('test.txt', 'rU') >>> f <open file 'test.txt', mode 'rU' at 0x63380> >>> you can read any file but text files are easiest text files can be created in Notepad or IDLE (or Word) the 'rU' is for universal newline mode (for now just always remember to put it in!) Girls’ Programming Network Reading files Mini-lecture 10 open reading Examples Summary TODO 4 A missing file causes an error here we try to open a file that doesn’t exist: 1 2 3 4 5 >>> f = open('missing.txt', 'rU') Traceback (most recent call last): File "<stdin>", line 1, in <module> IOError: [Errno 2] No such file or directory: 'missing.txt' >>> the result is an input/output error (an IOError exception) Girls’ Programming Network Reading files Mini-lecture 10 open reading Examples Summary TODO 5 Where is the file? The file is assumed to be in the current directory If not, you need to explicitly give the absolute path You specify an absolute path starting with a drive (e.g. C:) 1 >>> f = open('C:/test.txt', 'rU') opens up C:\test.txt regardless of where the program runs Girls’ Programming Network Reading files Mini-lecture 10 open An example file called 1 2 3 reading Examples Summary TODO 6 test.txt This is the first line in the file. This is the second line in the file. This is the third line in the file. Girls’ Programming Network Reading files Mini-lecture 10 open reading Examples Summary TODO 7 You can read a whole file into a string 1 2 3 4 5 6 7 8 >>> f = open('test.txt', 'rU') >>> s = f.read() >>> s 'This is the first line in the file.\nThis is the second lin >>> print s This is the first line in the file. This is the second line in the file. This is the third line in the file. 9 10 >>> the read method reads in the whole file Girls’ Programming Network Reading files Mini-lecture 10 open reading Examples Summary TODO 8 You can also read in one line at a time and all it takes is a for loop 1 2 3 4 5 >>> f = open('test.txt', 'rU') >>> for line in f: ... print line ... This is the first line in the file. 6 7 This is the second line in the file. 8 9 This is the third line in the file. 10 11 >>> Why is there an extra blank line each time? Girls’ Programming Network Reading files Mini-lecture 10 open reading Examples Summary TODO 9 Chomping off the newline The newline character is represented by '\n': 1 2 3 4 >>> print 'Hello\nWorld' Hello World >>> It is kept on each line during reading We can remove it lots of ways: 1 2 3 4 5 6 >>> x = 'abc\n' >>> x.strip() 'abc' >>> x[:-1] 'abc' >>> Girls’ Programming Network Reading files Mini-lecture 10 open reading Examples Summary TODO 10 Reading each line of a file 1 2 3 4 5 6 7 >>> for line in open('test.txt', 'rU'): ... line = line.strip() ... print line This is the first line in the file. This is the second line in the file. This is the third line in the file. >>> Girls’ Programming Network Reading files Mini-lecture 10 open reading Examples Summary TODO 11 Reading a file by columns 1 2 3 4 5 6 7 >>> for line in open('test.txt', 'rU'): ... cols = line.split() ... print cols[3] first second third >>> Girls’ Programming Network Reading files Mini-lecture 10 open reading Examples Summary TODO 12 Reading a file by words 1 2 3 4 5 6 7 8 9 10 >>> for line in open('test.txt', 'rU'): ... for word in line.split(): ... print word This is the first line ... >>> Girls’ Programming Network Reading files Mini-lecture 10 open reading Examples Summary TODO 13 You should now be able to: Open files in Python Read the whole file into a string Read each line of a file one at a time Girls’ Programming Network Reading files Mini-lecture 10 open reading Examples Summary TODO 14 Try these: create a short text file in Notepad read it in line and word at a time in Python Download Pride and Prejudice from Project Gutenberg print it out in Python count the number of lines count the number of lines containing Elizabeth print it out word at a time count the number of times the word Darcy appears find the most frequent word (this one’s a bit hard!) Girls’ Programming Network Reading files Mini-lecture 10