![]() ![]() Afterwards, we’ll use the appropriate method to read in the file- csv.reader() or json.load()-while printing an error message and exiting if the file does not have the appropriate extension. We use the input function to ask the user for the file name and determine the extension by taking the last section when split based on a period (. Now let’s flesh out asking for the filename and loading the file. Assuming nothing goes wrong, the else section will execute once the try section is completed. If you’re still unsure about how the code block works, the try section will execute and if anything goes wrong the except section will take place. exiting:",e) exit() else: # convert the file and save the output ![]() Here’s the outlined structure: try: # ask for the filename # load data except Exception as e: # print error message, exit script print("Error opening file. So we’re going to use a try/except/else statement to control the flow of our script. We could do a simple input, however, if the file doesn’t exist, the rest of our script won’t work. From here, the next step is to ask for the file to load. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |