Fundamentals of Programming(Python)
File Processing
Ali TaheriSharif University of Technology
Fall 2018
Outline1. Sources of Input
2. Files
3. Opening a File
4. Opening Modes
5. Closing a File
6. Writing to a File
7. Reading from a File
2ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2018
Sources of InputUser Input◦ Slow if needed to enter a lot of data
◦ Error prone: user may enter wrong values
◦ What if we want to run again?
◦ What if we want an input based on inputs of previous executions.
Files◦ Enter data once into a file, save it, and reuse it.
◦ Good for large amounts of data
◦ Programs can use files to communicate
◦ Need to be able to read from and write to files
3ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2018
FilesIn Python, a file operation takes place in the following order:1. Open the file
2. Read or write (perform operation)
3. Close the file
Files can be opened for different purposes◦ Reading, writing, updating
Open files must be closed◦ To release the resources
◦ To flush the output
4ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2018
Opening a FileTo access a file, it must first be opened
The open function is used to open a file◦ <variable> = open(<path>, <mode>)◦ <variable> is of type file (file is a data type!)
◦ <path> is the path and name of the file
◦ <mode> is the opening mode (read, write, update)
Example:
5ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2018
f = open('output.txt', 'w')
f = open('C:/Python36/README.txt', 'r')
Opening Modes
6ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2018
Mode Description
‘r’ Open a file for reading (default).
‘w’Open a file for writing. Creates a new file if it does not exist or truncates the file if it exists.
‘a’Open for appending at the end of the file without truncating it. Creates a new file if it does not exist.
‘b’ Open in binary mode.
‘+’ Open a file for updating (reading and writing)
f = open("test.txt") # equivalent to 'r'
f = open("test.txt",'r+') # read from begin, write at end
f = open("img.bmp",'wb') # write in binary mode
Closing a FileWhen we are done with operations to the file, we need to properly close the file
The close method is used to close the file
It can be automatically done using with statement
7ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2018
f = open('output.txt', 'w')
# some operations here
f.close()
with open('data.txt', 'a+') as f:
# some operations here
# now the file has been closed
Writing to a FileThe write method is used to output data to a file
◦ Slow if needed to enter a lot of data
◦ Error prone: user may enter wrong values
◦ We must include the newline characters ourselves to distinguish different lines.
This can be done using the print function as well
◦ The print function adds a new line to the end of the string by default
8ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2018
with open('data.txt', 'w') as f:
f.write("my first file\n")
f.write("This file\n\n")
f.write("contains four lines\n")
print('Sample text', file=f)
Reading from a FileWhen a file is opened for reading, an input pointer is positioned at the beginning of the file
After reading, the pointer is moved after the last read character
Reading in the text mode returns data in string
When the input pointer reaches the end of file, any further read attempts results in an empty string
9ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2018
Reading from a FileReading Characters◦ The read method is used to read characters from a
file
Reading Lines◦ The readline method is used to read a single line of text
◦ The lineReturns empty string when reaches end of file
◦ contains ending newline character ‘\n’
10ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2018
with open('input.txt', 'r') as f:
s = f.read(10) # read 10 characters
s = f.read() # read to the end of file
S = f.readline() # read the next line
Reading from a FileReading Lines◦ The readlines method returns all the lines as a list
◦ All the lines contain ending newline character ‘\n’
◦ We can also iterate over lines using a for loop
◦ Each line contain ending newline character ‘\n’
11ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2018
with open('input.txt', 'r') as f:
lines = f.readlines() # returns all lines as a list
for line in f:
print(line, end='')
Binary FilesWriting to a file◦ We can use pickle module to operations on binary files
◦ Using dump function, we can write a variable to a file
Reading from a file◦ We can use load function to read the variable in a file
12ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2018
import pickle
var = [(1, 'x', [3,4]), {1,2,3}]
with open(‘file.txt', ‘wb') as f:
pickle.dump(var, f) # Writes “var” to “file.txt”
with open(‘file.txt', ‘rb') as f:
print(pickle.load(f)) # Reads the variable in “file.txt”