2. Using Files for Data Storage
• Allows data to be retained between program runs
▫ It’s lost when a local variable goes out of scope or when the
Computers store files on secondary storage devices
hard disks, optical disks, flash drives and magnetic tapes.
Data maintained in files is persistent data because it exists beyond
the duration of program execution.
• Used in many applications:
▫ Word processing, Image Editors, Databases, Spreadsheets,
Compilers, Games and Web Browsers
• File: a set of data stored on a computer, often on a disk drive
▫ Programs can read from and/or write to files
▫ Open the file
▫ Use the file (read from, write to, or both)
▫ Close the file
3. Data Hierarchy
The smallest data item in a computer can assume the value
0 or 1.
Such data items are called bits
Python uses Unicode characters that are composed of two
bytes, each composed of eight bits(lang)
Fields are composed of characters or bytes.
Data items processed by computers form a data hierarchy
that becomes larger and more complex in structure as we
progress from bits to characters to fields, and so on.
• Typically, several fields compose a record (implemented
as a class in Java).
• A file is a group of related records. Database
4. Opening a File Stream
• The first part of read/write a file is to create a
stream to the file.
▫ A stream is a one way path for information to
▫ Code: varName = open(fileName, mode)
fileWrite = open(“test.txt”,”w”)
5. Writing Files
• To write to a file use the file stream name like a
variable with the write function.
▫ Code: write(str)
output = open(“city.txt”)
output.write(“blah blah blahn”)
Note: Loops can be used to write numerous values
to a file
6. Reading Files
• Reading a file is similar to writing a file in that
you use the file stream name followed by one of
the read functions.
▫ Code: read() – reads all characters from a file
read(#) – read a number of characters
readline() – reads all characters up to and
readlines() – reads all lines
7. Reading Data From a File
• Two main ways to read data from a file:
▫ 1. While loop that iterates to the end of the file
Code: line = infile.readline()
while line != ‘’:
#process line here
line = infile.readline()
▫ 2. A enhanced for loop
Code: for varname in fileName
#process the line here
8. Closing a File Stream
• Clean up is a necessary and essential step.
• Just like when you make a mess, life will go on
but there are implications of not cleaning up.
• Ensure you close a stream after you have
processed all of your data.
▫ Code: fileStream.close()
9. Reading and Writing Files Example
# Prompt the user to enter filenames
f1 = input("Enter a source file: ").strip()
f2 = input("Enter a target file: ").strip()
# Open files for input and output
infile = open(f1, "r")
outfile = open(f2, "w")
# Copy from input file to output file
countLines = countChars = 0
for line in infile:
countLines += 1
countChars += len(line)
print(countLines, "lines and", countChars, "chars copied")
infile.close() # Close the input file
outfile.close() # Close the output file
main() # Call the main function
• 1. What are the 3 phases to a file stream?
• 2. What are some different ways to setup your
• 3. What are the key differences between a read
and write file stream?