You can use Python isfile() and exists() methods of os module to check if a file or folder exists in Python. Here is a short example of checking whether a file or directory exists in Python –
1 2 3 4 5 6 7 8 9 10 11 12 |
>>> os.path.isfile("/etc/password.txt") True >>> os.path.isfile("/etc") False >>> os.path.isfile("/does/not/exist") False >>> os.path.exists("/etc/password.txt") True >>> os.path.exists("/etc") True >>> os.path.exists("/does/not/exist") False |