Week 13 Files
Week 13 Files
2
What is File I/O?
• One solution is to read the information from a
file on your computer
– You could even write information to a file
3 https://www.codecademy.com/courses/python-intermediate-en-OGNHh/0/1
File I/O Example Usage
• “Read” in a file using a word processor
– File opened
– Contents read into memory (RAM)
– File closed
– IMPORTANT: Changes to the file are
made to the copy stored in memory,
not the original file on the disk
4
File I/O Example Usage
• “Write” a file using a word processor
– (Saving a word processing file)
– Original file on the disk is reopened in a
mode that will allow writing
• This actually erases the old contents!
– Copy the version of the document stored in
memory to the original file on disk
– File is closed
5
File Processing
• In order to do some operations with files, we
need to be able to perform certain operations:
– Associate an external file with a program object
• Opening the file
– Manipulate the file object
• Reading from or writing to the file object
– Close the file
• Making sure the object and file match
6
Syntax: Opening a File
Syntax for open() Function
myFile = open(FILE_NAME [, ACCESS_MODE][, BUFFERING])
FILE_NAME
8
Syntax for open() Function
myFile = open(FILE_NAME [, ACCESS_MODE][, BUFFERING])
9
Syntax for open() Function
myFile = open(FILE_NAME [, ACCESS_MODE][, BUFFERING])
10
Examples of Using open()
• In general, we will use commands like:
testFile = open("scores.txt")
dataIn = open("old_stats.dat")
dataOut = open("stats.dat", "w")
• We will ignore the optional buffering argument
scores.txt
an example 2.5 8.1 7.6 3.2 3.2
input file 3.0 11.6 6.5 2.7 12.4
8.0 8.0 8.0 8.0 7.5
11
File Processing: Reading
Using File Objects to Read Files
myFile = open("myStuff.txt")
14
Entire Contents into One String
>>> info = open("hours.txt")
>>> wholeThing = info.read()
>>> wholeThing
'123 Susan 12.5 8.1 7.6 3.2\n456 Brad 4.0
11.6 6.5 2.7 12\n789 Jenn 8.0 8.0 8.0 8.0
7.5\n'
19
A Better Way to Read One Line at a Time
• Instead of reading them manually, use a
for loop to iterate through the file line by line
>>> info = open("hours.txt")
>>> for eachLine in info: why are there all
... print(eachLine)
these empty lines???
...
123 Susan 12.5 8.1 7.6 3.2 now that we’re
calling print(),
456 Brad 4.0 11.6 6.5 2.7 12
the \n is printing
789 Jenn 8.0 8.0 8.0 8.0 7.5 out as a new line
20
Whitespace
Whitespace
• Whitespace is any “blank” character, that
represents space between other characters
• For example: tabs, newlines, and spaces
"\t" "\n" " "
22
Removing the Newline from the End
• To remove the escaped newline sequence (\n)
from a string we read in, we can use slicing
myString = myString[:-1]
0 1 2 3
d o g \n
-4 -3 -2 -1
myString
23
Removing the Newline from the End
• To remove the escaped newline sequence (\n)
from a string we read in, we can use slicing
myString = myString[:-1]
don’t remove 0 1 2 3
anything from the
beginning d o g \n
just remove the very -4 -3 -2 -1
last character myString
24