Python Programming 19CSE282
Python Programming 19CSE282
Python Programming 19CSE282
19CSE282
What You Need In Order To Read
Informa4on From A File
1. Open the file and associate the file with a file
variable.
2. A command to read the informa4on.
3. A command to close the file.
1. Opening Files
Prepares the file for reading:
A. Links the file variable with the physical file (references to the file variable are
references to the physical file).
B. Posi4ons the file pointer at the start of the file.
Format:1
<file variable> = open(<file name>, "r")
fhand = open(‘test’, “r”)
Example:
(Constant file name)
inputFile = open("data.txt", "r")
OR
(Variable file name: entered by user at run4me)
filename = input("Enter name of input file: ")
inputFile = open(filename, "r")
1 Examples assume that the file is in the same directory/folder as the Python program.
2. Reading Informa4on From Files
• Typically reading is done within the body of a loop
• Each execu4on of the loop will read a line from the file into
a string
Print(fhand.readline())
Format:
for <variable to store a string> in <name of file variable>:
<Do something with the string read from file>
Example:
for line in inputFile:
print(line) # Echo file contents back onscreen
Closing The File
• Although a file is automa4cally closed when your program
ends it is s4ll a good style to explicitly close your file as soon
as the program is done with it.
• What if the program encounters a run4me error and crashes before
it reaches the end? The input file may remain ‘locked’ an
inaccessible state because it’s s4ll open.
• Format:
<name of file variable>.close()
• Example:
inputFile.close()
Reading From Files: PuTng It All
Together
Name of the online example: grades1.py
Input files: letters.txt or gpa.txt
inputFileName = input("Enter name of input file: ")
inputFile = open(inputFileName, "r")
print("Opening file", inputFileName, " for reading.")
for line in inputFile:
sys.stdout.write(line)
inputFile.close()
print("Completed reading of file", inputFileName)
What You Need To Write
Informa4on To A File
1. Open the file and associate the file with a file
variable (file is “locked” for wri4ng).
2. A command to write the informa4on.
3. A command to close the file.
1. Opening The File
Format1:
<name of file variable> = open(<file name>, "w")
Example:
(Constant file name)
outputFile = open("gpa.txt", "w")
(Variable file name: entered by user at run4me)
outputFileName = input("Enter the name of the output file
to record the GPA's to: ")
outputFile = open(outputFileName, "w")
1 Typically the file is created in the same directory/folder as the Python program.
3. Wri4ng To A File
• You can use the ‘write()’ func4on in conjunc4on with a file variable.
• Note however that this func4on will ONLY take a string parameter
(everything else must be converted to this type first).
Format:
outputFile.write(temp)
Example:
# Assume that temp contains a string of characters.
outputFile.write (temp)
Wri4ng To A File: PuTng It All Together
• Name of the online example: grades2.py
• Input file: “letters.txt” (sample output file name: gpa.txt)