1 / 18

CSE 231 Lab 5

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 ”

cowles
Download Presentation

CSE 231 Lab 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 231 Lab 5

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

  3. Open file to read/write What is “r”? • file_obj= open(filename_str,"r") • file_obj_out= open(filename2_str,"w") 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 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. 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. 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. 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+'' 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

  14. 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 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

More Related