180 likes | 197 Views
CSE 231 Lab 5. Topics to cover. Files: open file to read/write exceptions. O pen file to read/write. What is “r”?. file_obj = open (filename_ str ," r ") file_obj_out = open (filename2_str," w "). What is “w”?. Files in Python – r mode. file_name = “input.txt ”
E N D
Topics to cover Files: open file to read/write exceptions
Open file to read/write What is “r”? • file_obj= open(filename_str,"r") • file_obj_out= open(filename2_str,"w") What is “w”?
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
Files in Python – r mode 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???
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()
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)
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!
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!
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?
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?
Open file modes • ''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 • '' 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. • Start at the beginning of the file. • ''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
File Opening in Python the_file= open(“test_oops.txt”, “r”) # test_oops.txt file does not exit What will happen?
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.
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
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
Testing for types • Example: Check if the string input is a float. Create your own function is_float(s) to check for floats defis_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