Thứ Sáu, 14 tháng 2, 2014

07-Files

Using open()

handle = open(filename, mode)

returns a handle use to manipulate the file

filename is a string

mode is optional and should be 'r' if we are planning reading the file
and 'w' if we are going to write to the file.
http://docs.python.org/lib/built-in-funcs.html
fhand = open('mbox.txt', 'r')
What is a Handle?
>>> fhand = open('mbox.txt')
>>> print fhand
<open file 'mbox.txt', mode 'r' at 0x1005088b0>
When Files are Missing
>>> fhand = open('stuff.txt')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'stuff.txt'
The newline
Character

We use a special character to
indicate when a line ends
called the "newline"

We represent it as \n in strings

Newline is still one character -
not two
>>> stuff = 'Hello\nWorld!'
>>> stuff
'Hello\nWorld!'
>>> print stuff
Hello
World!
>>> stuff = 'X\nY'
>>> print stuff
X
Y
>>> len(stuff)
3
File Processing

A text file can be thought of as a sequence of lines
From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
Return-Path: <postmaster@collab.sakaiproject.org>
Date: Sat, 5 Jan 2008 09:12:18 -0500
To: source@collab.sakaiproject.org
From: stephen.marquard@uct.ac.za
Subject: [sakai] svn commit: r39772 - content/branches/
Details: http://source.sakaiproject.org/viewsvn/?view=rev&rev=39772
File Processing

A text file has newlines at the end of each line
From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008\n
Return-Path: <postmaster@collab.sakaiproject.org>\n
Date: Sat, 5 Jan 2008 09:12:18 -0500\n
To: source@collab.sakaiproject.org\n
From: stephen.marquard@uct.ac.za\n
Subject: [sakai] svn commit: r39772 - content/branches/\n
Details: http://source.sakaiproject.org/viewsvn/?view=rev&rev=39772\n
File Handle as a Sequence

A file handle open for read can be
treated as a sequence of strings
where each line in the file is a string
in the sequence

We can use the for statement to
iterate through a sequence

Remember - a sequence is an
ordered set
xfile = open('mbox.txt', 'r')
for cheese in xfile:
print cheese
Counting Lines in a File

Open a file read-only

Use a for loop to read each
line

Count the lines and print out
the number of lines
fhand = open('mbox.txt')
count = 0
for line in fhand:
count = count + 1
print 'Line Count:', count
python open.py
Line Count: 132045
Reading the *Whole* File

We can read the whole file
(newlines and all) into a
single string.
>>> fhand = open('mbox-short.txt')
>>> inp = fhand.read()
>>> print len(inp)
94626
>>> print inp[:20]
From stephen.marquar
Searching Through a File

We can put an if statement in
our for loop to only print
lines that meet some criteria
fhand = open('mbox-short.txt')
for line in fhand:
if line.startswith('From:') :
print line

Xem chi tiết: 07-Files


Không có nhận xét nào:

Đăng nhận xét