Week 14

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 11

SWE225

Introduction to
Programming and Problem Solving
COLLEGE OF TECHNOLOGICAL INNOVATIONS (CTI)
Topics of Discussion
• Writing to files
• Reading from files
Introduction
• Before you read from or write to a file in Python, you need to create a handle to
that file. A handle is somewhat like a TV remote control

• We may ask a handle to do:


• Read a line
• Read all lines in file
• Write a line
• Write multiple lines

• There are three stages:


• Open the file
• Read from /write to file
• Close the file
Reading the Complete File
• The function open creates a # open hello.txt to read from
handle to a file. Parameters are: handle = open ("hello.txt", "r")
• file name
• access mode # read all content of the file
• File name should include path if text = handle.read( )
files are not in the same folder # print variable text to screen
• Access mode is specified by a print( text )
letter: r = read, whereas w = write) # close file
handle.close( )
Reading one Line
• The function readline reads one # open hello.txt to read from
line from an opened file handle = open("hello.txt", "r")
# read one line from the file
• If you call readline again, it will read line = handle.readline( )
the second line of the file and so on
# print line to screen
print( line )
# close file
handle.close( )
Reading All Lines as a List
You can use readlines( ) to read all # open hello.txt to read from
lines as a list of strings as: handle = open("hello.txt", "r")
# read all lines from the file
linesList = handle.readlines( )
linesList = handle.readlines( )
# print line to screen
print( linesList )
# close file
handle.close( )
Reading Line by Line
# open hello.txt to read from
• You can use a loop to read all lines handle = open("hello.txt", "r")
of a file, one by one for line in handle:
# print line to screen
• Simplest way is to use a for in loop, print( line )
where in each iteration you access
one line # close hello.txt
handle.close( )
Writing a Line to a File
• To write to a file, you need to use # open hello.txt to read from
the open function and specify handle = open("hello.txt", "w")
access mode to be "w"
# write a line to the file
handle.write("I like Python!")
• If the file does not exist, Python will
create it for you # close hello.txt
handle.close( )
• If the file exists, Python will clear it
and start at the beginning
Writing Multiple Lines Iteratively
• Use a loop to write multiple lines to linesList = [ ”Salam Students\n",
a file "I want to practice Python\n",
"Files operations are easy\n"]
handle = open( "hello.txt", "w" )
for line in linesList :
# write a line to the file
handle.write( line )
# close hello.txt
handle.close( )
Writing Multiple Lines at once
• You can write multiple lines at lines = [ " Salam Students \n",
once using writelines function "I need to practice Python\n",
"Files operations are easy\n"]
• If the file does not exist, Python # open hello.txt to read from
will create it for you handle = open( "hello.txt", "w" )
# write a line to the file
• If the file exists, Python will clear handle.writelines( lines )
it and start at the beginning # close hello.txt
handle.close( )
Appending a Line to a File
• To open a file and append lines to lines = [ "Salam Students \n",
it, the access mode should be "a" "I need to practice Python\n",
"Files operations are easy\n"]
• Python will add whatever lines # open hello.txt to read from
you write to the end of the file handle = open( "hello.txt", "a" )
# append lines to the file
handle.writelines( lines )
# close hello.txt
handle.close( )

You might also like