Home How to check whether a file exists?

# How to check whether a file exists?

spence91
1#
spence91 Published in 2008-09-17 12:55:00Z
 How to see if a file exists or not, without using the try statement?
abc
2#
 import os.path if os.path.isfile(filepath):
abc
3#
 import os os.path.exists(path) # returns whether the path (dir or file) exists or not os.path.isfile(path) # returns whether the file exists or not
EOL
4#
 You have the os.path.exists function: import os.path os.path.exists(file_path) This returns True for both files and directories but you can instead use os.path.isfile to test if it's a file specifically. It follows symlinks.
user2357112
5#
 If the reason you're checking is so you can do something like if file_exists: open_it(), it's safer to use a try around the attempt to open it. Checking and then opening risks the file being deleted or moved or something between when you check and when you try to open it. If you're not planning to open the file immediately, you can 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) if you need to be sure it's a file. Starting with Python 3.4, the pathlib module offers an object-oriented approach (backported to pathlib2 in Python 2.7): from pathlib import Path my_file = Path("/path/to/file") if my_file.is_file(): # file exists To check a directory, do: if my_file.is_dir(): # directory exists To check whether a Path object exists independently of whether is it a file or directory, use exists(): if my_file.exists(): # path exists You can also use resolve() in a try block: try: my_abs_path = my_file.resolve(): except FileNotFoundError: # doesn't exist else: # exists
armatita
6#
 Additionally, os.access(): if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() Being R_OK, W_OK, and X_OK the flags to test for permissions (doc).
Aaron Hall
7#
Aaron Hall Reply to 2016-12-02 19:33:06Z
 Unlike isfile(), exists() will return True for directories. So depending on if you want only plain files or also directories, you'll use isfile() or exists(). Here is a simple REPL output. >>> print os.path.isfile("/etc/password.txt") True >>> print os.path.isfile("/etc") False >>> print os.path.isfile("/does/not/exist") False >>> print os.path.exists("/etc/password.txt") True >>> print os.path.exists("/etc") True >>> print os.path.exists("/does/not/exist") False
Honest Abe
8#
Honest Abe Reply to 2014-04-28 01:01:59Z
 Prefer the try statement. It's considered better style and avoids race conditions. Don't take my word for it. There's plenty of support for this theory. Here's a couple: Style: Section "Handling unusual conditions" of http://allendowney.com/sd/notes/notes11.txt Avoiding Race Conditions
Peter Mortensen
9#
Peter Mortensen Reply to 2017-05-27 00:43:51Z
 You could try this (safer): try: # http://effbot.org/zone/python-with-statement.htm # 'with' is safer to open a file with open('whatever.txt') as fh: # Do something with 'fh' except IOError as e: print("({})".format(e)) The ouput would be: ([Errno 2] No such file or directory: 'whatever.txt') Then, depending on the result, your program can just keep running from there or you can code to stop it if you want.
Honest Abe
10#
Honest Abe Reply to 2014-09-18 04:39:52Z
 import os path = /path/to/dir root,dirs,files = os.walk(path).next() if myfile in files: print "yes it exists" This is helpful when checking for several files. Or you want to do a set intersection/ subtraction with an existing list.
octopusgrabbus
11#
 This sample function will test for a file's presence in a very Pythonic way using try .. except: def file_exists(filename): try: with open(filename) as f: return True except IOError: return False
Peter Mortensen
12#
Peter Mortensen Reply to 2017-05-27 00:45:02Z
 You can simply use the tempfile module to know whether a file exists or not: import tempfile tempfile._exists('filename') # Returns True or False
user2154354
13#
 You should definitely use this one. from os.path import exists if exists("file") == True: print "File exists." elif exists("file") == False: print "File doesn't exist."
MrWonderful
14#
 This is the simplest way to check if a file exists. Just because the file existed when you checked doesn't guarantee that it will be there when you need to open it. import os fname = "foo.txt" if os.path.isfile(fname): print("file does exist at this time") else: print("no such file exists at this time")
15#
 It doesn't seem like there's a meaningful functional difference between try/except and isfile(), so you should use which one makes sense. If you want to read a file, if it exists, do try: f = open(filepath) except IOError: print 'Oh dear.' But if you just wanted to rename a file if it exists, and therefore don't need to open it, do if os.path.isfile(filepath): os.rename(filepath, filepath + '.old') If you want to write to a file, if it doesn't exist, do # python 2 if not os.path.isfile(filepath): f = open(filepath, 'w') # python 3, x opens for exclusive creation, failing if the file already exists try: f = open(filepath, 'wx') except IOError: print 'file already exists' If you need file locking, that's a different matter.
codeforester
16#
 If you want to do that in Bash it would be: if [ -e "$FILE" ]; then prog "$FILE" fi Which I sometimes do when using Python to do more complicated manipulation of a list of names (as I sometimes need to use Python for), the try open(file): except: method isn't really what's wanted, as it is not the Python process that is intended to open the file. In one case, the purpose is to filter a list of names according to whether they exist at present (and there are no processes likely to delete the file, nor security issues since this is on my Raspberry Pi which has no sensitive files on its SD card). I'm wondering whether a 'Simple Patterns' site would be a good idea? So that, for example, you could illustrate both methods with links between them and links to discussions as to when to use which pattern.
Cody Piersall
17#
Cody Piersall Reply to 2017-12-04 14:45:12Z
 Python 3.4+ has an object-oriented path module: pathlib. Using this new module, you can check whether a file exists like this: import pathlib p = pathlib.Path('path/to/file') if p.is_file(): # or p.is_dir() to see if it is a directory # do stuff You can (and usually should) still use a try/except block when opening files: try: with p.open() as f: # do awesome stuff except OSError: print('Well darn.') The pathlib module has lots of cool stuff in it: convenient globbing, checking file's owner, easier path joining, etc. It's worth checking out. If you're on an older Python (version 2.6 or later), you can still install pathlib with pip: # installs pathlib2 on older Python versions # the original third-party module, pathlib, is no longer maintained. pip install pathlib2 Then import it as follows: # Older Python versions import pathlib2 as pathlib
Peter Mortensen
18#
Peter Mortensen Reply to 2017-05-27 00:48:31Z
 You can write Brian's suggestion without the try:. from contextlib import suppress with suppress(IOError), open('filename'): process() suppress is part of Python 3.4. In older releases you can quickly write your own suppress: from contextlib import contextmanager @contextmanager def suppress(*exceptions): try: yield except exceptions: pass
Peter Mortensen
19#
Peter Mortensen Reply to 2017-05-27 00:48:49Z
 You can use the following open method to check if a file exists + readable: open(inputFile, 'r')
20#
 If the file is for opening you could use one of the following techniques: >>> with open('somefile', 'xt') as f: #Using the x-flag, Python3.3 and above ... f.write('Hello\n') >>> if not os.path.exists('somefile'): ... with open('somefile', 'wt') as f: ... f.write("Hello\n") ... else: ... print('File already exists!')
Hanson
21#
 To check if a file exists, from sys import argv from os.path import exists script, filename = argv target = open(filename) print "file exists: %r" % exists(filename)
Peter Mortensen
22#
Peter Mortensen Reply to 2017-05-27 00:49:25Z
 You can use the "OS" library of Python: >>> import os >>> os.path.exists("C:\\Users\\####\\Desktop\\test.txt") True >>> os.path.exists("C:\\Users\\####\\Desktop\\test.tx") False
Peter Mortensen
23#
Peter Mortensen Reply to 2017-05-27 00:52:18Z
 Although I always recommend using try and except statements, here are a few possibilities for you (my personal favourite is using os.access): Try opening the file: Opening the file will always verify the existence of the file. You can make a function just like so: def File_Existence(filepath): f = open(filepath) return True If it's False, it will stop execution with an unhanded IOError or OSError in later versions of Python. To catch the exception, you have to use a try except clause. Of course, you can always use a try except` statement like so (thanks to hsandt for making me think): def File_Existence(filepath): try: f = open(filepath) except IOError, OSError: # Note OSError is for later versions of Python return False return True Use os.path.exists(path): This will check the existence of what you specify. However, it checks for files and directories so beware about how you use it. import os.path >>> os.path.exists("this/is/a/directory") True >>> os.path.exists("this/is/a/file.txt") True >>> os.path.exists("not/a/directory") False Use os.access(path, mode): This will check whether you have access to the file. It will check for permissions. Based on the os.py documentation, typing in os.F_OK, it will check the existence of the path. However, using this will create a security hole, as someone can attack your file using the time between checking the permissions and opening the file. You should instead go directly to opening the file instead of checking its permissions. (EAFP vs LBYP). If you're not going to open the file afterwards, and only checking its existence, then you can use this. Anyway, here: >>> import os >>> os.access("/is/a/file.txt", os.F_OK) True I should also mention that there are two ways that you will not be able to verify the existence of a file. Either the issue will be permission denied or no such file or directory. If you catch an IOError, set the IOError as e (like my first option), and then type in print(e.args) so that you can hopefully determine your issue. I hope it helps! :)
Pedro Lobito
24#
Pedro Lobito Reply to 2015-07-18 17:41:08Z
 if os.path.isfile(path_to_file): try: open(path_to_file) pass except IOError as e: print "Unable to open file" Raising exceptions is considered to be an acceptable, and Pythonic, approach for flow control in your program. Consider handling missing files with IOErrors. In this situation, an IOError exception will be raised if the file exists but the user does not have read permissions. SRC: http://www.pfinn.net/python-check-if-file-exists.html
Bishop
25#
 import os #Your path here e.g. "C:\Program Files\text.txt" #For access purposes: "C:\\Program Files\\text.txt" if os.path.exists("C:\..."): print "File found!" else: print "File not found!" Importing os makes it easier to navigate and perform standard actions with your operating system. For reference also see How to check whether a file exists using Python? If you need high-level operations, use shutil.
bubble
26#
 import os.path def isReadableFile(file_path, file_name): full_path = file_path + "/" + file_name try: if not os.path.exists(file_path): print "File path is invalid." return False elif not os.path.isfile(full_path): print "File does not exist." return False elif not os.access(full_path, os.R_OK): print "File cannot be read." return False else: print "File can be read." return True except IOError as ex: print "I/O error({0}): {1}".format(ex.errno, ex.strerror) except Error as ex: print "Error({0}): {1}".format(ex.errno, ex.strerror) return False #------------------------------------------------------ path = "/usr/khaled/documents/puzzles" fileName = "puzzle_1.txt" isReadableFile(path, fileName)
Aaron Hall
27#
Aaron Hall Reply to 2018-02-10 14:05:23Z

## How do I check whether a file exists, using Python, without using a try statement?

Now available since Python 3.4, import and instantiate a Path object with the file name, and check the is_file method (note that this returns True for symlinks pointing to regular files as well):

>>> from pathlib import Path
>>> Path('/').is_file()
False
>>> Path('/initrd.img').is_file()
True
>>> Path('/doesnotexist').is_file()
False

If you're on Python 2, you can backport the pathlib module from pypi, pathlib2, or otherwise check isfile from the os.path module:

>>> import os
>>> os.path.isfile('/')
False
>>> os.path.isfile('/initrd.img')
True
>>> os.path.isfile('/doesnotexist')
False

Now the above is probably the best pragmatic direct answer here, but there's the possibility of a race condition (depending on what you're trying to accomplish), and the fact that the underlying implementation uses a try, but Python uses try everywhere in its implementation.

The rest of this answer attempts to consider these caveats.

## Longer, much more pedantic answer

Available since Python 3.4, use the new Path object in pathlib. Note that .exists is not quite right, because directories are not files (except in the unix sense that everything is a file).

>>> from pathlib import Path
>>> root = Path('/')
>>> root.exists()
True

So we need to use is_file:

>>> root.is_file()
False

Here's the help on is_file:

is_file(self)
Whether this path is a regular file (also True for symlinks pointing
to regular files).

So let's get a file that we know is a file:

>>> import tempfile
>>> file = tempfile.NamedTemporaryFile()
>>> filepathobj = Path(file.name)
>>> filepathobj.is_file()
True
>>> filepathobj.exists()
True

By default, NamedTemporaryFile deletes the file when closed (and will automatically close when no more references exist to it).

>>> del file
>>> filepathobj.exists()
False
>>> filepathobj.is_file()
False

If you dig into the implementation, though, you'll see that is_file uses try:

def is_file(self):
"""
Whether this path is a regular file (also True for symlinks pointing
to regular files).
"""
try:
return S_ISREG(self.stat().st_mode)
except OSError as e:
if e.errno not in (ENOENT, ENOTDIR):
raise
# Path doesn't exist or is a broken symlink
# (see https://bitbucket.org/pitrou/pathlib/issue/12/)
return False

## Race Conditions: Why we like try

We like try because it avoids race conditions. With try, you simply attempt to read your file, expecting it to be there, and if not, you catch the exception and perform whatever fallback behavior makes sense.

If you want to check that a file exists before you attempt to read it, and you might be deleting it and then you might be using multiple threads or processes, or another program knows about that file and could delete it - you risk the chance of a race condition if you check it exists, because you are then racing to open it before its condition (its existence) changes.

Race conditions are very hard to debug because there's a very small window in which they can cause your program to fail.

But if this is your motivation, you can get the value of a try statement by using the suppress context manager.

## Avoiding race conditions without a try statement: suppress

Python 3.4 gives us the suppress context manager (previously the ignore context manager), which does semantically exactly the same thing in fewer lines, while also (at least superficially) meeting the original ask to avoid a try statement:

from contextlib import suppress

with suppress(OSError), open(path) as f:

or

with suppress(OSError):
os.remove(path)

Usage:

>>> with suppress(OSError), open('doesnotexist') as f:
...
>>>
>>> with suppress(OSError):
...     os.remove('doesnotexist')
...
>>>

For earlier Pythons, you could roll your own suppress, but without a try will be more verbose than with. I do believe this actually is the only answer that doesn't use try at any level in the Python that can be applied to prior to Python 3.4 because it uses a context manager instead:

class suppress(object):
def __init__(self, *exceptions):
self.exceptions = exceptions
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
if exc_type is not None:
return issubclass(exc_type, self.exceptions)

Perhaps easier with a try:

from contextlib import contextmanager

@contextmanager
def suppress(*exceptions):
try:
yield
except exceptions:
pass

## Other options that don't meet the ask for "without try":

isfile

import os
os.path.isfile(path)

from the docs:

os.path.isfile(path)

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.

But if you examine the source of this function, you'll see it actually does use a try statement:

# This follows symbolic links, so both islink() and isdir() can be true
# for the same path on systems that support symlinks
def isfile(path):
"""Test whether a path is a regular file"""
try:
st = os.stat(path)
except os.error:
return False
return stat.S_ISREG(st.st_mode)
>>> OSError is os.error
True

All it's doing is using the given path to see if it can get stats on it, catching OSError and then checking if it's a file if it didn't raise the exception.

If you intend to do something with the file, I would suggest directly attempting it with a try-except to avoid a race condition:

try:
with open(path) as f:
except OSError:
pass

os.access

Available for Unix and Windows is os.access, but to use you must pass flags, and it does not differentiate between files and directories. This is more used to test if the real invoking user has access in an elevated privilege environment:

import os
os.access(path, os.F_OK)

It also suffers from the same race condition problems as isfile. From the docs:

Note: Using access() to check if a user is authorized to e.g. open a file before actually doing so using open() creates a security hole, because the user might exploit the short time interval between checking and opening the file to manipulate it. It’s preferable to use EAFP techniques. For example:

if os.access("myfile", os.R_OK):
with open("myfile") as fp:
return "some default data"

is better written as:

try:
fp = open("myfile")
except IOError as e:
if e.errno == errno.EACCES:
return "some default data"
# Not a permission error.
raise
else:
with fp:

Avoid using os.access. It is a low level function that has more opportunities for user error than the higher level objects and functions discussed above.

Love and peace - Joe Codeswell
28#
Love and peace - Joe Codeswell Reply to 2015-08-29 16:15:29Z
 Here's a 1 line Python command for the Linux command line environment. I find this VERY HANDY since I'm not such a hot Bash guy. python -c "import os.path; print os.path.isfile('/path_to/file.xxx')" I hope this is helpful.
Peter Mortensen
29#
Peter Mortensen Reply to 2017-05-27 01:01:07Z
 In Python 3.4 the language provides a new module to manage files: import pathlib path = pathlib.Path('path/to/file') if path.is_file(): # If you want to check a directory: path.is_dir() # If it is true, return true on your code.
Peter Mortensen
30#
Peter Mortensen Reply to 2017-05-27 01:00:22Z
 In 2016 the best way is still using os.path.isfile: >>> os.path.isfile('/path/to/some/file.txt') Or in Python 3 you can use pathlib: import pathlib path = pathlib.Path('/path/to/some/file.txt') if path.is_file(): ...
 You need to login account before you can post.
Processed in 0.466271 second(s) , Gzip On .