Checking If a File Exists

Last updated 8 months ago

How to use a custom test to check if a file exists.

Sometimes you may want to check that a file exists, but not care about (or know) the contents of it. In the latter case you could use a code pattern, and in this guide we'll accomplish the former with a custom test.

Here's the custom test creation form filled out with the required values:

You can read about most of these values here — the only important one for this guide are the Test Contents, shown below:

import os.path

os.path.exists(<filename>) returns True if a file exists, otherwise False . assertwill trigger an AssertionError if passed a falsey value which will be caught by the Failed Pattern, and the test will fail.

To wrap up, save your test, and add it to a requirement.