Download presentation
Presentation is loading. Please wait.
1
Copyright (c) 2017 by Dr. E. Horvath
FILES-Methods to Read In the first set of PowerPoint slides on file access, you learned just one way to read from a file; essentially you used a loop to iterate through the lines in a file. There are three methods that can be used to read from a file: readline, readlines, and read(size_in_bytes). Copyright (c) 2017 by Dr. E. Horvath
2
Copyright (c) 2017 by Dr. E. Horvath
The readline method Use the temp1.txt data file that was created when you ran the code that was included in the first set of PowerPoint slides on files. The following snippet of code uses the readline method to read in one line of code. try: the_temp_file = open("temp1.txt","r") print(the_temp_file.readline().strip()) the_temp_file.close() except FileNotFoundError: print("The file wasn't found") Copyright (c) 2017 by Dr. E. Horvath
3
More on the readline method
The following snippet of code uses the readline method to read in every line. try: the_temp_file = open("temp1.txt","r") for i in range(0,10): print(the_temp_file.readline().strip()) the_temp_file.close() except FileNotFoundError: print("The file wasn't found") Copyright (c) 2017 by Dr. E. Horvath
4
More on the readline method
The following snippet of code uses the readline method to read in every line. try: the_temp_file = open("temp1.txt","r") for i in range(0,10): print(the_temp_file.readline().strip()) the_temp_file.close() except FileNotFoundError: print("The file wasn't found") Copyright (c) 2017 by Dr. E. Horvath
5
More on the readline method
The following snippet of code uses the readline method to read in every line. This code isn't well designed because you would have to know the length of the file in advance in order to hard code the number of iterations. The iterative technique discussed in the previous set of slides accomplishes the same goal and does so neatly. try: the_temp_file = open("temp1.txt","r") for i in range(0,10): print(the_temp_file.readline().strip()) the_temp_file.close() except FileNotFoundError: print("The file wasn't found") Copyright (c) 2017 by Dr. E. Horvath
6
Copyright (c) 2017 by Dr. E. Horvath
The readlines method The readlines method reads in every line of code and stores each line as a separate element in a list. This is a convenient method to use because the reading in and storing of the data is handled in one line of code. try: the_temp_file = open("temp1.txt","r") the_temp_list = the_temp_file.readlines() the_temp_file.close() except FileNotFoundError: print("The file wasn't found") Copyright (c) 2017 by Dr. E. Horvath
7
Reading Files One Byte at a Time
The read(size_in_bytes) method allows the programmer to read data in blocks of bytes. The following snippet of code reads in the first twenty characters one at a time: try: the_temp_file = open("temp1.txt","r") for i in range(0,20): print(the_temp_file.read(1)) the_temp_file.close() except FileNotFoundError: print("The file wasn't found") Copyright (c) 2017 by Dr. E. Horvath
8
Reading Files One Byte at a Time Cont'd
To read in more than one byte at a time, simply change the argument of the read method to that number of bytes: try: the_temp_file = open("temp1.txt","r") for i in range(0,20): print(the_temp_file.read(3)) the_temp_file.close() except FileNotFoundError: print("The file wasn't found") Copyright (c) 2017 by Dr. E. Horvath
9
Reading Files One Byte at a Time Cont'd
The method read(), without any argument, causes the rest of the file to be read in: try: the_temp_file = open("temp1.txt","r") for i in range(0,20): print(the_temp_file.read(3)) print(the_temp_file.read()) the_temp_file.close() except FileNotFoundError: print("The file wasn't found") Copyright (c) 2017 by Dr. E. Horvath
10
The write() Method to Write to Files
The following code prompts the user to enter a grocery item in an endless. The user breaks out of the loop by pressing ctrl-c, and the code catches the KeyboardInterrupt. grocery_file = open("groceries.txt","w") try: while True: user_response = input("Enter a product: ") grocery_file.write(user_response) except KeyboardInterrupt: pass grocery_file.close() Copyright (c) 2017 by Dr. E. Horvath
11
The write() method cont'd.
The problem with the previous code is that each item the user enters is tacked onto one long string without any line breaks. The following code fixes that problem by adding a plus sign and '\n', which is the symbol for newline: grocery_file = open("groceries.txt","w") try: while True: user_response = input("Enter a product: ") grocery_file.write(user_response + '\n') except KeyboardInterrupt: pass grocery_file.close() Copyright (c) 2017 by Dr. E. Horvath
12
The writelines() Method
A list can be written to a file using one file. The following snippet of code reads data in from the groceries.txt file using the readlines() method, converts each item to upper case, and uses the writelines() method to write the list to a new file, ugroceries.txt. try: grocery_file = open("groceries.txt","r") grocery_list = grocery_file.readlines() for item in range(0,len(grocery_list)): print(grocery_list[item].upper()) ugrocery_file = open("ugroceries.txt","w") ugrocery_file.writelines(grocery_list) except FileNotFoundError: print("File not found")) Copyright (c) 2017 by Dr. E. Horvath
13
Copyright (c) 2017 by Dr. E. Horvath
Seek and tell Python provides two methods for moving around files. The tell() method tells the current position in bytes and the seek(byte_pos) method moves to the position specified. The following code opens the groceries.txt file, reads in a couple of lines, and then uses the tell() method to find the current location. try: grocery_file = open("groceries.txt","r") grocery_list = grocery_file.readline() current_position = grocery_file.tell() print(current_position) grocery_file.close() except FileNotFoundError: pass Copyright (c) 2017 by Dr. E. Horvath
14
Copyright (c) 2017 by Dr. E. Horvath
Seek and Find The following code opens the groceries.txt file and uses the seek(byte) method to change the current position in the file. Passing an argument of 0 causes the program to move to the beginning of the file, and the expression seek(0,2) causes the program to move to the end of the file. try: grocery_file = open("groceries.txt","r") grocery_file.seek(45) grocery_item = grocery_file.readline() print(grocery_item) current_position = grocery_file.tell() print(current_position) grocery_file.close() except FileNotFoundError: pass Copyright (c) 2017 by Dr. E. Horvath
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.