Checking whether a file or directory exists

If you want to check whether a file exists in the current working path directory, you can use the os.path.exists() function, passing the file or directory you want to check as the parameter:

 >>> import os >>> os.path.exists("./main.py") True >>> os.path.exists("./not_exists.py") False

Get Mastering Python for Networking and Security now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.