0% found this document useful (0 votes)
8 views6 pages

SDF Week14

Uploaded by

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

SDF Week14

Uploaded by

housamelgouina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

3/12/2024

Software Development Fundamentals


Week 14

Dr. Loubna Mekouar

Note:
• Some of the slides of the course are based on the material provided
by the College of Technological Information, Zayed University, UAE.
• The copyrighted material belongs to their respective owners.

1
3/12/2024

Introduction
• Before you read from or write to a file in Python, you need to create a
handle to that file.
• 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( )

2
3/12/2024

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( )

3
3/12/2024

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

4
3/12/2024

Writing Multiple Lines Iteratively


• Use a loop to write multiple lines to linesList = [ ”Salam Students\n",
a file "I want you 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( )
9

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 lines to the file
• If the file exists, Python will clear handle.writelines( lines )
it and start at the beginning # close hello.txt
handle.close( )
10

10

5
3/12/2024

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( )
11

11

You might also like