0% found this document useful (0 votes)
10 views23 pages

Reading Files in Python

The document discusses reading and processing text files in Python. It covers opening files, using file handles, reading files line by line, counting lines, searching for strings, stripping whitespace, and handling errors. Key points include using open() to get a file handle, iterating over the file handle in a for loop to read lines, and using string methods like startswith(), strip(), and in to select lines meeting certain criteria.

Uploaded by

muhammadjaziem25
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views23 pages

Reading Files in Python

The document discusses reading and processing text files in Python. It covers opening files, using file handles, reading files line by line, counting lines, searching for strings, stripping whitespace, and handling errors. Key points include using open() to get a file handle, iterating over the file handle in a for loop to read lines, and using string methods like startswith(), strip(), and in to select lines meeting certain criteria.

Uploaded by

muhammadjaziem25
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Reading Files

Chapter 7
Software What
It is time to go find some
Next? Data to mess with!
Input Central
and Output Processing Files R
Devices Unit Us

Secondary
if x < 3: print Memory

Main From [Link]@[Link] Sat Jan 5 09:14:16 2008


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

Details: [Link]

[Link]
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 with the open() function

• open() returns a “file handle” - a variable used to perform


operations on the file

• Similar to “File -> Open” in a Word Processor


Using open()
fhand = open('[Link]', 'r')
• 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 to


read the file and 'w' if we are going to write to the file
What is a Handle?
>>> fhand = open('[Link]')
>>> print(fhand)
<_io.TextIOWrapper name='[Link]' mode='r' encoding='UTF-8'>
When Files are Missing
>>> fhand = open('[Link]')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '[Link]'
The newline Character
>>> stuff = 'Hello\nWorld!'
>>> stuff
• We use a special character 'Hello\nWorld!'
called the “newline” to indicate >>> print(stuff)
when a line ends Hello
World!
• We represent it as \n in strings >>> stuff = 'X\nY'
>>> print(stuff)
X
• Newline is still one character -
Y
not two >>> len(stuff)
3
File Processing
A text file can be thought of as a sequence of lines

From [Link]@[Link] Sat Jan 5 09:14:16 2008


Return-Path: <postmaster@[Link]>
Date: Sat, 5 Jan 2008 09:12:18 -0500
To: source@[Link]
From: [Link]@[Link]
Subject: [sakai] svn commit: r39772 - content/branches/

Details: [Link]
File Processing
A text file has newlines at the end of each line

From [Link]@[Link] Sat Jan 5 09:14:16 2008\n


Return-Path: <postmaster@[Link]>\n
Date: Sat, 5 Jan 2008 09:12:18 -0500\n
To: source@[Link]\n
From: [Link]@[Link]\n
Subject: [sakai] svn commit: r39772 - content/branches/\n
\n
Details: [Link]
Reading Files in Python
File Handle as a Sequence
• A file handle open for read can
be treated as a sequence of
strings where each line in the xfile = open('[Link]')
file is a string in the sequence for cheese in xfile:
print(cheese)
• We can use the for statement
to iterate through a sequence

• Remember - a sequence is an
ordered set
Counting Lines in a File
fhand = open('[Link]')
• Open a file read-only count = 0
for line in fhand:
• Use a for loop to read each line count = count + 1
print('Line Count:', count)
• Count the lines and print out
the number of lines
$ python [Link]
Line Count: 132045
Reading the *Whole* File
>>> fhand = open('[Link]')
We can read the whole >>> inp = [Link]()
file (newlines and all) >>> print(len(inp))
into a single string 94626
>>> print(inp[:20])
From [Link]
Searching Through a File

We can put an if statement in fhand = open('[Link]')


for line in fhand:
our for loop to only print lines
if [Link]('From:') :
that meet some criteria print(line)
OOPS!
From: [Link]@[Link]
What are all these blank
lines doing here? From: louis@[Link]

From: zqian@[Link]

From: rjlowe@[Link]
...
OOPS!
What are all these blank From: [Link]@[Link]\n
lines doing here? \n
From: louis@[Link]\n
• Each line from the file \n
has a newline at the end From: zqian@[Link]\n
\n
• The print statement adds From: rjlowe@[Link]\n
a newline to each line \n
...
Searching Through a File (fixed)
fhand = open('[Link]')
• We can strip the whitespace for line in fhand:
from the right-hand side of line = [Link]()
if [Link]('From:') :
the string using rstrip() from print(line)
the string library
From: [Link]@[Link]
• The newline is considered
From: louis@[Link]
“white space” and is From: zqian@[Link]
stripped From: rjlowe@[Link]
....
Skipping with continue
fhand = open('[Link]')
We can conveniently for line in fhand:
skip a line by using the line = [Link]()
if not [Link]('From:') :
continue statement continue
print(line)
Using in to Select Lines
fhand = open('[Link]')
We can look for a string for line in fhand:
anywhere in a line as our line = [Link]()
if not '@[Link]' in line :
selection criteria continue
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]
Author: [Link]@[Link]
From [Link]@[Link] Fri Jan 4 07:02:32 2008
X-Authentication-Warning: set sender to [Link]@[Link] using -f...
fname = input('Enter the file name: ')
fhand = open(fname)
count = 0
Prompt for
for line in fhand:
if [Link]('Subject:') :
count = count + 1
File Name
print('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]
fname = input('Enter the file name: ')
try:

Bad File fhand = open(fname)


except:
print('File cannot be opened:', fname)

Names quit()

count = 0
for line in fhand:
if [Link]('Subject:') :
count = count + 1
print('There were', count, 'subject lines in', fname)

Enter the file name: [Link]


There were 1797 subject lines in [Link]

Enter the file name: na na boo boo


File cannot be opened: na na boo boo
Summary
• Secondary storage • Searching for lines

• Opening a file - file handle • Reading file names

• File structure - newline character • Dealing with bad files

• Reading a file line by line with a


for loop

Common questions

Powered by AI

Error handling is crucial in file operations to manage situations where files are not accessible due to incorrect file names, lack of permissions, or if files do not exist. In Python, this is often handled using try-except blocks. For example, when attempting to open a file that does not exist, an exception like FileNotFoundError can be caught, allowing the program to alert the user and handle the error gracefully without crashing. This ensures robustness and reliability in file-handling scripts .

In Python, the open() function is essential for file input/output operations, enabling files to be opened in different modes such as 'r' for reading and 'w' for writing. File handles are used to perform operations on files. For reading, methods like read(), readline(), and readlines() are used, each offering different advantages in terms of memory usage and simplicity. Iterating through a file object via a for loop allows processing each line sequentially with low memory overhead. These operations allow flexible file management, supporting both line-by-line processing and reading the whole file content .

Prompting users for file names is advantageous as it allows the script to handle varying input file scenarios without hard-coding filenames. This increases script flexibility and usability. To handle incorrect file names, incorporate try-except blocks in file opening operations to catch errors and notify users about incorrect inputs. This approach prevents the program from crashing and improves user experience by providing clear feedback about the nature of the error and allows users to correct their input .

Handling missing files in Python is important to prevent runtime errors and allow graceful degradation of the program when files are not found. This can be encoded using try-except blocks where attempts to open a file that may be missing are wrapped in a try block. If the file is not found, an IOError is caught in the except clause, where the programmer can log an error message and exit or redirect the user. This approach maintains program stability and provides user feedback about file-related issues .

Python's file processing capabilities can be leveraged to extract and count specific information by reading files line-by-line and applying filters or conditions on lines of interest. For example, to count subject lines in an email file, open the file, and iterate over each line checking if a line starts with 'Subject:'. Each time a match occurs, increment a counter variable. This selective processing allows efficient data extraction and manipulation based on specific patterns or criteria within the file .

Efficiency in reading large files can be maximized by iterating over files line-by-line instead of loading the entire file into memory at once. This is done by treating the file handle as an iterable object in a for loop. This approach minimizes memory usage by processing one line at a time, which is particularly beneficial for large files. Additionally, using generators for complex file processing tasks can further optimize performance by consuming only the necessary computations without additional overhead .

The 'newline' character, represented as '\n', is used to indicate the end of a line in a text file. In Python, when reading files, each line is read with this newline character included. This affects file processing because it ensures that lines are separated properly when files are read line-by-line. However, when printing lines, the print function adds an additional newline, causing extra blank lines between outputs unless the newline is stripped using functions like rstrip().

The 'continue' statement impacts logic flow by skipping the rest of the loop body for the current iteration and proceeding to the next iteration. This is particularly useful in file processing when we only want to process certain lines based on specific criteria and ignore others. For instance, if searching for lines starting with 'From:', each line that does not meet this condition can be skipped using 'continue'. This ensures that only lines starting with 'From:' are processed further or printed .

Methods like rstrip() enhance the accuracy of file content processing by removing whitespace, including newline characters, from the end of strings. This is particularly useful for processing files line-by-line, as it removes extraneous characters that could interfere with comparisons and output formatting, thus avoiding additional blank lines or inaccuracies when matching patterns or keywords in file contents .

File modes in Python's open() function specify the intent of the file operation. Mode 'r' opens a file for reading, 'w' opens a file for writing (truncating the file first if it exists), and 'a' opens a file in append mode, allowing data to be added to the end of the file without truncating it. Understanding these modes is essential for managing file operations correctly, as using the wrong mode can lead to data loss if files are accidentally overwritten .

You might also like