Lecture 16 – Open, read, write and close files
COMPSCI 101Principles of Programming
2COMPSCI 101 - Principles of Programming
At the end of this lecture, students should be able to: understand file structure open and close a file read data from a file write data to a file
Learning outcomes
3COMPSCI 101 - Principles of Programming
Add the integer values in two lists of any size.
Recap
def add_lists_V2(list1, list2): new_list=[] if len(list1) < len(list2): length = len(list2) else: length = len(list1)
for i in range(length): if i < len(list1) and i < len(list2): new_list = new_list + [ list1[i] + list2[i] ] elif len(list1) < len(list2): new_list = new_list + [ list2[i] ] else: new_list = new_list + [ list1[i] ] return new_list
def input_integer_list(): nums = input("Enter the integers: ") num_list = nums.split()
# continued from the left for i in range(len(num_list)): num_list[i] = int(num_list[i])
return num_list
def main(): print("Add two lists of any size.") list1 = input_integer_list() list2 = input_integer_list() new_list = add_lists_V2(list1, list2) print("Sum of two lists:", new_list)
main()
Add two lists of any size.Enter integers: 12 34 5 90 76 24 45Enter integers: 24 34 45 6Sum of two lists: [36, 68, 50, 96, 76, 24, 45]
4COMPSCI 101 - Principles of Programming
What is a File?
Data used in a program is temporary, and it is lost when the program terminates.
To permanently store the data created in a program, we need to save it on a physical storage device.
A file is a collection of bytes of information that usually resides permanently on a disk.
The data in a file can be used later by other programs.Accessing a file means establishing a connection between
the file and a program and moving data between them.Read data from a file into a programWrite data from a program to a file
5COMPSCI 101 - Principles of Programming
Accessing a File
When set up a connection between the Python program and a file, a steam of data flow is established between the two.
6CompSci 101 - Principles of Programming
Accessing a file The file system of a computer organizes
files in a hierarchical (tree) structure. Files are placed in directories. Directories can contain files or other directories.
A complete description of which directories to visit in order to reach a certain file is called a path, e.g.,
Each path to a file or a directory must be unambiguous.
C:\Users\Jing\Documents\file1.py
C:\
Python3 Users
Jing
Documents
file1.py file2.py input.txt
7CompSci 101 - Principles of Programming
Path of a file The file path is the '\' separated list of directories which
need to be visited in order to reach the file. For example, if the file, input.txt, needs to be accessed
inside the program, file2.py, it can be accessed by the absolute path:
or by the relative path:
'C:\Users\Jing\Documents\input.txt'
'input.txt'
C:\
Python3 Users
Jing
Documents
file1.py file2.py input.txt
8CompSci 101 - Principles of Programming
Binary vs text filesPython files are classified into two categories, i.e., text and
binary. text files can be processed using a text editor.binary files are designed to be read by programs and that consist
of sequences of binary digits, e.g., images, audio, video files. If you open a binary file using a text editor, the editor tries to
match the binary information into text characters, but mostly the file content is not readable.
File displayed by a text editor
File displayed by an image viewer
9COMPSCI 101 - Principles of Programming
Processing a FileProcessing a file consists of these three steps:
Opening a file for reading or writingReading from the file and/or writing to the fileClosing the file
Open a File Read/Write Close the File
10COMPSCI 101 - Principles of Programming
The syntax for opening a file is as follows:
Example:
Opening a File
input_file = open("stocks.txt", "r")
fileVariable = open(filename, mode)
Opens a file in the working directory
11COMPSCI 101 - Principles of Programming
Closing a FileThe syntax for closing a file is as follows:
The close() function closes the file (i.e., releases the file resources). After a file has been closed, the access to the file contents is no longer available (until it is reopened). If the mode is the “write” mode, any as yet unwritten content is
flushed to the file.Example:
fileVariable.close()
def main():input_file = open("stocks.txt", "r")# processing the fileinput_file.close()
main()
12COMPSCI 101 - Principles of Programming
Writing to a FileFirst, the file must be opened for writing:
If output.txt does not exist, the open() function will create it, If output.txt does exist, its content will be erased.
The syntax for writing a file is as follows:
Example:
Close the file after writing.
output_file = open("output.txt", "w")
fileVariabe.write(string)
output_file.write("BC001,Fresh toast bread white (700g),3.99,20\n")output_file.write("BC002,Low-fat milk (2 liter),4.80,10\n");output_file.close()
BC001,Fresh toast bread white (700g),3.99,20BC002,Low-fat milk (2 liter),4.80,10
13COMPSCI 101 - Principles of Programming
A file must be opened and use the “a” mode for appending data.
Example:
Close the file after writing.
Appending Data
output_file = open("output.txt", "a")
def main(): output_file = open("output.txt", "a") output_file.write("BC003,V-energy drink,2.75,10\n") output_file.write("BC004,Fresh garlic (450g),1.98,0\n"); output_file.write("Coca-Cola (300 ml),2.50,10\n"); output_file.close()
main()BC001,Fresh toast bread white (700g),3.99,20BC002,Low-fat milk (2 liter),4.80,10BC003,V-energy drink,2.75,10BC004,Fresh garlic (450g),1.98,0BC005,Coca-Cola (300 ml),2.50,10
14CompSci 101 - Principles of Programming
Writing to a file continued The syntax for writing to a file:
and the parameter passed to the write() function is a string. Any integers need to be converted using the str() function. Any new lines need to be written to the file as the symbol "\n”. For example:
fileVariabe.write(string)
def main():output_file = open("output.txt", "w")sum_of_nums = int(input("Enter num: "))sum_of_nums += int(input("Enter num: "))output_file.write(str(sum_of_nums) + "\n")output_file.close()
main()
15CompSci 101 - Principles of Programming
Complete the functionComplete the write_to_file() function which writes
the elements of the two parameter lists (one element from both lists per line). The elements are separated by ": ".def write_to_file(filename, list1, list2): # Add your code here
def main(): a_list1 = [2, 4, 5, 6, 8, 1] a_list2 = [123, 54, 58, 106, 87, 206] filename = "combined_lists" write_to_file(filename, a_list1, a_list2)
main()
Assume the two lists have exactly the same number of elements and that each element is an integer.
16CompSci 101 - Principles of Programming
Complete the functionComplete the write_to_file() function which writes
the elements of the two parameter lists (one element from both lists per line). The elements are separated by ": ".def write_to_file(filename, list1, list2): output_file = open(filename, "w") for i in range(len(list1)): output_file.write(str(list1[i]) + ": " + str(list2[i]) + "\n") output_file.close()
def main(): a_list1 = [2, 4, 5, 6, 8, 1] a_list2 = [123, 54, 58, 106, 87, 206] filename = "combined_lists" write_to_file(filename, a_list1, a_list2)
main()
Assume the two lists have exactly the same number of elements and that each element is an integer.
17COMPSCI 101 - Principles of Programming
There are three different ways that an opened file can be read.
Reading a File
Mode Descriptionread(int) Return the specified numbers of
characters from the file.If the argument is omitted, the entire remaining content in the file are read.
readline() Returns the next line of the file as a string.
readlines() Returns a list of the remaining lines in the file.
18CompSci 101 - Principles of Programming
The read() functionsThe read() function returns the entire contents of the file. This function returns a string.
The read(an_integer) function returns the specified number of characters (a string) from the file.
all_contents = input_file.read()
some_characters = input_file.read(an_integer)
19CompSci 101 - Principles of Programming
The read() functions - examples Both the following sections of code use the file below:
input_file = open("poem.txt", "r")all_contents = input_file.read()print(all_contents)
A thing of beauty is a joy for ever: Its loveliness increases; it will never Pass into nothingness; ...
John Keat
input_file = open("poem.txt", "r")some_contents = input_file.read(10)print(some_contents)print(len(some_contents))
A thing of10
Note that the file needs to be closed
20CompSci 101 - Principles of Programming
The readline()/readlines() functionsThe readline() function returns the next line of the file. This function returns a string. A trailing newline character ("\n”) is kept in the string.
The readlines() function returns a list of the remaining lines of the file. This function returns a list of strings.
next_line = input_file.readline()
list_of_lines = input_file.readlines()
21CompSci 101 - Principles of Programming
The readline()/readlines() functions - examples Both the following sections of code use the file below:
input_file = open("RedHerring.txt", "r")one_line = input_file.readline()print(one_line)
A Red Herring: A distraction from the main issue.
input_file = open("RedHerring.txt", "r")list_of_lines = input_file.readlines()print(list_of_lines[2])print(list_of_lines[4])print(len(list_of_lines)
A red herring has a strong odour.
the smell of the herring and start
6
Note that the string read from the text contains the newline character.
22COMPSCI 101 - Principles of Programming
ExerciseComplete the copy_file() function which takes the
names of an input file and an output file, copies data from the input file to the output file and returns the number of characters in the file.Open the input file and read its content as a stringOpen a new output file and write the content string.
def copy_file(file1, file2): # Add your code here
def main(): length = copy_file("stock.txt", "stock2.txt") print("File copied, size is " + str(length) + " characters.")
main()
File copied, size is 641 characters.
23COMPSCI 101 - Principles of Programming
SummaryFiles are used to store data permanently from a program on
storage devices. File objects are used to read/write data from/to files. Open a file creates a file object with certain access mode,
e.g., “r” for reading, “w” for writing, and “a” for appending.When opening a file with the write mode, if the file already
exists, its content is erased, otherwise, a new file is created.The read(), readline(), and readlines() methods are used to
read the data from a file, while the write() method is used to write data into a file.
You should always close the file after it is processed to ensure that the data is saved properly.
24COMPSCI 101 - Principles of Programming
Files need to be opened before can read or write to them f = open( filename[, mode] )
Files should be closed when you finish with them. f.close()
Reading a file f.read() f.read( int ) f.readline() f.readlines()
Writing a file f.write( string )
Python features used in this lecture
Top Related