Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 16 – Open, read, write and close files.  At the end of this lecture, students should be able to:  understand file structure  open and close.

Similar presentations


Presentation on theme: "Lecture 16 – Open, read, write and close files.  At the end of this lecture, students should be able to:  understand file structure  open and close."— Presentation transcript:

1 Lecture 16 – Open, read, write and close files

2  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 2COMPSCI 101 - Principles of Programming

3  Add the integer values in two lists of any size. 3COMPSCI 101 - Principles of Programming 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 45 Enter integers: 24 34 45 6 Sum of two lists: [36, 68, 50, 96, 76, 24, 45]

4  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 program  Write data from a program to a file COMPSCI 101 - Principles of Programming4

5  When set up a connection between the Python program and a file, a steam of data flow is established between the two. COMPSCI 101 - Principles of Programming5

6  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. 6CompSci 101 - Principles of Programming C:\Users\Jing\Documents\file1.py C:\Python3UsersJing Documentsfile1.pyfile2.pyinput.txt

7  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: 7CompSci 101 - Principles of Programming 'C:\Users\Jing\Documents\input.txt' 'input.txt' C:\Python3UsersJing Documentsfile1.pyfile2.pyinput.txt

8  Python 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. 8CompSci 101 - Principles of Programming File displayed by a text editor File displayed by an image viewer

9  Processing a file consists of these three steps:  Opening a file for reading or writing  Reading from the file and/or writing to the file  Closing the file Open a FileRead/Write Close the File COMPSCI 101 - Principles of Programming9

10  The syntax for opening a file is as follows:  Example: input_file = open("stocks.txt", "r") fileVariable = open(filename, mode) Opens a file in the working directory COMPSCI 101 - Principles of Programming10

11  The 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 file input_file.close() main() def main(): input_file = open("stocks.txt", "r") # processing the file input_file.close() main() COMPSCI 101 - Principles of Programming11

12  First, 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() 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,20 BC002,Low-fat milk (2 liter),4.80,10 COMPSCI 101 - Principles of Programming12

13  A file must be opened and use the “a” mode for appending data.  Example:  Close the file after writing. 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() 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,20 BC002,Low-fat milk (2 liter),4.80,10 BC003,V-energy drink,2.75,10 BC004,Fresh garlic (450g),1.98,0 BC005,Coca-Cola (300 ml),2.50,10 COMPSCI 101 - Principles of Programming13

14  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: 14CompSci 101 - Principles of Programming 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() 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()

15  Complete 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 ": ". 15CompSci 101 - Principles of Programming 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() 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.

16  Complete 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 ": ". 16CompSci 101 - Principles of Programming 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() 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.

17  There are three different ways that an opened file can be read. COMPSCI 101 - Principles of Programming17 ModeDescription read(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.

18  The 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. 18CompSci 101 - Principles of Programming all_contents = input_file.read() some_characters = input_file.read(an_integer)

19  Both the following sections of code use the file below: 19CompSci 101 - Principles of Programming input_file = open("poem.txt", "r") all_contents = input_file.read() print(all_contents) 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)) input_file = open("poem.txt", "r") some_contents = input_file.read(10) print(some_contents) print(len(some_contents)) A thing of 10 Note that the file needs to be closed

20  The 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. 20CompSci 101 - Principles of Programming next_line = input_file.readline() list_of_lines = input_file.readlines()

21  Both the following sections of code use the file below: 21CompSci 101 - Principles of Programming input_file = open("RedHerring.txt", "r") one_line = input_file.readline() print(one_line) 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) 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.

22  Complete 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 string  Open 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() 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. COMPSCI 101 - Principles of Programming22

23  Files 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. COMPSCI 101 - Principles of Programming23

24  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 ) 24COMPSCI 101 - Principles of Programming


Download ppt "Lecture 16 – Open, read, write and close files.  At the end of this lecture, students should be able to:  understand file structure  open and close."

Similar presentations


Ads by Google