Spring Deals - hours only!Up to 70% off on all courses and bundles.-Close
The very basics of text files
with statement and exception handling
9. Exceptions


All right! Now that you know two ways of working with text files, let's move on and perform a tiny experiment: what happens when you try to open a file that doesn't exist?


Take a look at the template code. Provide a non-existent file name – magic_file.txt – as the argument of open(), and run the code.

As you can see, Python shows an error. It doesn't look very nice, and we don't really want our users to see something like this if we're writing an application. That's why we need to learn how to handle such situations properly.

Click I'm done. Next exercise to continue.