Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 231 Lab 5.

Similar presentations


Presentation on theme: "CSE 231 Lab 5."— Presentation transcript:

1 CSE 231 Lab 5

2 Files: open file to read/write exceptions
Topics to cover Files: open file to read/write exceptions

3 Open file to read/write
file_obj = open(filename_str,"r") file_obj_out = open(filename2_str,"w") What is “r”? What is “w”?

4 Files in Python – r mode file_name = “input.txt” the_file = open(file_name, “r”) for line in the_file: print(line) the_file.close() #Read one line at a time input.txt First Line Second Line Third Line

5 Files in Python – r mode Why the extra blank lines?
Every line ends with this character: ‘\n’ This indicates the END of a line and all files who have multiple lines have them. Why the extra blank lines? How do we get rid of whitespace???

6 Files in Python – r mode file_name = “input.txt” the_file = open(file_name, “r”) for line in the_file: # Get rid of only whitespaces at the end of the string line = line.strip() print(line) the_file.close()

7 print(“Hello World!”, file = file_obj_out)
Files in Python – w mode file_name = “input.txt” file_obj_out = open(file_name,"w" ) file_obj_out.write(“Hello World!”) file_name = “input.txt” file_obj_out = open(file_name,"w" ) print(“Hello World!”, file = file_obj_out)

8 Files in Python – w mode filename2_str = “test.txt”
file_obj_out = open(filename2_str,"w") # What is that “w”? print(“Hello World!”, file = file_obj_out) There is nothing in the file!

9 Open file to read/write
filename2_str = “test.txt” file_obj_out = open(filename2_str,"w") print(“Hello World!”, file = file_obj_out) file_obj_out.close() # ALWAYS CLOSE THE FILE!

10 Is this correct? Files in Python – w mode
the_file = open(“test.txt”, “w”) # What is that “w”? for line in the_file: print(line) the_file.close() Is this correct?

11 File Opening in Python – w mode

12 What will be printed? Files in Python – w mode
input_str="{:>10s} is {:<10d} years old".format("Bill",25) the_file = open(“test.txt”, “w”) print(‘First line’, file = the_file) print(‘Second line’, file = the_file) print(input_str, file = the_file) the_file.close() What will be printed?

13 Open file modes '' r'' reading only mode.
Start at the beginning of the file. ''w'' Writing only mode. Override the file if exists or create text file for writing. ''a'' Open for appending. The file is created if it does not exist. Start at the end of the file. Subsequent writes to the file will always end up at the current end of file ''r+'' Open for reading and writing. Start at the beginning of the file. ''w+'' Open for reading and writing. Override the file if exists or create text file for writing. Starts at the beginning of the file. ''a+'' Open for reading and appending. The file is created if it does not exist. Start at the end of the file. Subsequent writes to the file will always end up at the current end of file

14 What will happen? File Opening in Python
the_file = open(“test_oops.txt”, “r” ) # test_oops.txt file does not exit What will happen?

15 File Opening in Python – FileNotFoundError
Here we have a specific exception that was thrown, FileNotFoundError. We want to be able to catch that mistake and handle it.

16 What happens if the file doesn’t exist?
FileNotFoundError: [Errno 2] No such file or directory: ‘filename’ How to catch that exception: try: file_obj = open(filename_str,"r") except FileNotFoundError: print(“File doesn’t exist!”) You can do it in a loop to keep prompting the user for a valid filename Much safer and our code does not crash 

17 What happens if the file doesn’t exist?
How to catch exceptions: try: filename_str = input() file_obj = open(filename_str,"r") file_obj.close() except : #catch general exceptions print(“File doesn’t exist!”) You can do it in a loop to keep prompting the user for a valid filename Much safer and our code does not crash 

18 Testing for types Example: Check if the string input is a float. Create your own function is_float(s) to check for floats def is_float(s) try: float(s) # s is a string return True except ValueError : return False You can do it in a loop to keep prompting the user for a valid float


Download ppt "CSE 231 Lab 5."

Similar presentations


Ads by Google