Checking If a File Exists

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
assert(os.path.exists('foo.py'))

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 check, and add it to a task.