Introduction to
“File Processing”
1
Outline
• Introduction
• Opening a file
• Reading from a file
• Writing to a file
• Closing a file
• Reading from the Web
2
Introduction
• File Processing: is the process of reading or
writing to a file and manipulating its data.
• File processing operations include:
Opening a file
Reading from a file
Writing to a file
Closing the file
3
Opening a File
• Before we can read the contents of the file we must tell
Python which file we are going to work with and what we will
be doing with the file
• This is done using the open() function
• open() returns a “file handle” - a variable used to perform
operations on the file
• Kind of like “File -> Open” in a Word Processor
4
What is a Handle?
>>> fhand = open('[Link]')
>>> print (fhand)
<open file '[Link]', mode 'r' at 0x1005088b0>
5
Opening a file
• To open a file we use function
open(<file name>, <open mode>)
• Name is a string with the actual file name on the disk.
• Open mode can be
Read “r”
Write “w”
Append “a”
depending on whether we are reading, writing or appending in
the file.
• Example:
>>> f = open ("C:\\[Link]",'r')
<open file '[Link]', mode 'r' at 6
>>> print f 0x00000000021BEA50 >
>>> type (f)
Opening a file
• To open a file we use function
open(<file name>, <open mode>)
• Name is a string with the actual file name on the disk.
• Open mode can be
Read “r”
Write “w”
Append “a”
depending on whether we are reading, writing or appending in
Unless the file already exists,
the file. you will get an error:
• Example:
>>> fi = open ("C:\\[Link]",'r')
>>> print fi <open file '[Link]', mode 'r' at 7
0x00000000021BEA50 >
>>> type (fi)
<type 'file'>
Opening a file
• To open a file we use function
open(<file name>, <open mode>)
• Name is a string with the actual file name on the disk.
• Open mode can be
Read “r”
Write “w”
Append “a”
depending on whether we are reading, writing or appending in
the file.
• Example:
>>> fo = open ("C:\\[Link]",’w')
if the file does not exist, it will be created; 8
>>> print fo If the file already exists, it will overwrite its contents
>>> type (fo)
Opening a file
• To open a file we use function
open(<file name>, <open mode>)
• Name is a string with the actual file name on the disk.
• Open mode can be
Read “r”
Write “w”
Append “a”
depending on whether we are reading, writing or appending in
the file.
• Example:
>>> fo = open ("C:\\[Link]",’a')
if the file does not exist, it will be created;
>>> print fo If the file already exists, it will point at the beginning of its 9
>>> type (fo) contents waiting for your commands (read or write).
If you want to add then you need to move to its end to add
<type 'file'>
content.
Reading from a file
• How to read data from the file ?
• There are 3 different methods to read data from files
readline () returns the next line in the file as a string.
readlines () returns all lines in the file as a list of strings
(lines)
read() returns the entire file content as one string.
• Example:
>>> fin = open ( '[Link]' , 'r' )
If the file is empty, readline() and
>>> Line = [Link]() readlines() will return an emty
>>> Lines = [Link]() string and empty list.
10
[Link]
• Assume the following contents in [Link]
11
The newline
Character
• We use a special character
to indicate when a line >>> stuff = 'X\nY’
ends called the "newline" >>> print (stuff)
X
• We represent it as \n in
Y
strings >>> len (stuff)
• Newline is still one 3
character - not two
12
A closer look at reading lines
Removes any white spaces at the
beginning or end of the string.
13
A closer look at reading lines
Removes any white spaces at the
beginning or end of the string.
Notice how reading resumed from
the last point, rather than restarting
form the beginning of the file
14
Reading from a file (cont.)
• read() : reads the entire contents of the file
content as a string including “newline”
characters “\n”.
15
Reading from a file (cont.)
• Seek (offset) method: sets the file's current
position at the offset (character position).
Will start reading a line
from the 4th character in
the file.
will go back to the
beginning of the file!
Will start reading a line
from the 21th character in
the file.
16
Looping over the contents of a
file
17
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-
[Link]')
for line in fhand:
if
[Link]('From:') :
print (line)
18
Searching Through a File
(fixed)
fhand = open('[Link]')
• We can strip the for line in fhand:
whitespace from the right line = [Link]()
hand side of the string if [Link]('From:') :
using rstrip() from the print (line)
string library
• The newline is considered
From: [Link]@[Link]
"white space" and is From: louis@[Link]
stripped From: zqian@[Link]
From: rjlowe@[Link]
....
19
Using in to select lines
• We can look for a string fhand = open('[Link]')
anywhere in a line as for line in fhand:
our selection criteria line = [Link]()
if '@[Link]' in line :
print (line)
From [Link]@[Link] Sat Jan 5 09:14:16 2008
X-Authentication-Warning: set sender to [Link]@[Link] using –f
From: [Link]@[Link]: [Link]@[Link]
From [Link]@[Link] Fri Jan 4 07:02:32 2008
X-Authentication-Warning: set sender to [Link]@[Link] using -f...
20
Prompt for
name = input('Enter the file name: ')
hand = open(fname)
ount = 0
or line in fhand:
if [Link]('Subject:') :
File Name
count = count + 1
rint ('There were', count, 'subject lines in', fname)
Enter the file name: [Link]
There were 1797 subject lines in [Link]
Enter the file name: [Link]
There were 27 subject lines in [Link]
21
Closing files
• Whether you are reading or writing you always need to
close a file after you’re done processing it .
• In some cases, not properly closing a file could result in
data loss.
• To close a file , use the close( ) method
Example:
>>> [Link]()
22
Writing to a file
• To write to a file , use “w” open mode
What will happen to the file in “w” mode ??
File exists: clear the file’s contents.
File doesn’t exists: a new one is created.
• Example:
>>> f =open ("[Link]",'w')
>>> [Link] ("Hello")
When you are done writing, you
>>> [Link]() have to close the file to see the
updates 23
Writing to a file (Cont.)
• What if you want to write data to your file? Lets
take the example of wanting to write numbers!
• Example:
>>> f =open ("[Link]",'w')
>>> [Link] (7)
TypeError: expected a character buffer object
• Use the string formatting operators %
• Example:
>>> f =open ("[Link]",'w')
>>> n= 7 24
>>> [Link] ("this is number %d" %(n))
Writing to a file (Cont.)
• To append the file content without deleting the
original content, use “a” open mode
• Example:
>>> f =open ("[Link]",'a') “Hi” will be added to the file
>>> [Link] ("Hi") content without deleting the
previous data.
>>> [Link]()
• To write a new line in the file, use “ \n”
Example:
>>> f =open ("[Link]",'a')
>>> [Link] ("\n Welcome")
“Welcome” will be added in a
>>> [Link]() new line. 25
The flush method
• Data is usually buffered in memory before its
actually written to a file.
• You will notice that you will only see what you have
written to file after closing it.
• If you want to force the contents of the buffer to be
written to the file, you can use the flush method.
• Example:
• f =open ("[Link]",‘w') Open the file now. You’ll see
that its empty
• [Link](“hello")
• [Link]() Now, re-open the file. You’ll 26
see ‘hello’ has been written
Reading from URLs!!
import urllib
url = "[Link]
web_page = [Link](url)
for line in web_page:
line = [Link]()
print line
web_page.close()
27
• readline(n): Reads up to n characters from the
current line.
• What Happens: If the line contains fewer than n
characters, it reads the whole line.
• If n is larger than the number of characters in the
line, it stops at the newline \n and does not
proceed to the next line.
28
• read(n): Reads exactly n characters from the file.
• What Happens: Starts reading from the current
file pointer position and stops after n characters
(or the end of the file if fewer than n characters
remain).
29
• readlines(n): Reads approximately n bytes worth
of lines from the file.
• What Happens: Instead of reading one line or the
entire file, it tries to accumulate lines whose total
size is approximately n bytes.
• If the file's lines are short, it might read multiple
lines. If they are long, it may read only one line.
30