CHECK IF A FILE EXISTS USING PYTHON

Samuel Fernandes

Total Post:159

Points:1117
Posted by  Samuel Fernandes
File  Python 
 977  View(s)
Ratings:
Rate this:
How do I check if a file exists, using Python, without using a try statement?
  1. Mayank Tripathi

    Post:397

    Points:3117
    Re: Check if a file exists using Python

    You can also use os.path.isfile

    Return True if path is an existing regular file. This follows symbolic links, so both islink() and isfile() can be true for the same path.
    import os.path
    os.path.isfile(fname) 


    You have the os.path.exists function:

    import os.path
    os.path.exists(file_path)
    This returns True for both files and directories.

    Use os.path.isfile to test if it's a file specifically

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!