How do I check whether a file exists, without using the try statement?
Join Stack Overflow to learn, share knowledge, and build your career.
|
|||||||||||||
|
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.
|
|||||||||||||
|
|
2017 / 12 / 22: Although almost every possible way has been listed in (at least one of) the existing answers (e.g. Python 3.4 specific stuff was added), I'll try to group everything together. Note: every piece of Python standard library code that I'm going to post, belongs to version 3.5.3 (doc quotes are version 3 specific). Problem statement:
Possible solutions:
|
|||||||||||||||||||||
|
|
Date:2017-12-04 Every possible solution has been listed in other answers. An intuitive and arguable way to check if a file exists is the following:
I made an exhaustive cheatsheet for your reference:
|
|||||
|
|
Python 3.4+ has an object-oriented path module: pathlib. Using this new module, you can check whether a file exists like this:
You can (and usually should) still use a
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:
Then import it as follows:
|
|||||
|
|
In this case you can check file name exist in listdir or not. if not the simply file not exist.
This works perfect. |
|||
|
|
|
If you want to do that in Bash it would be:
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 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. |
||||
|
|
|
If the reason you're checking is so you can do something like If you're not planning to open the file immediately, you can use
if you need to be sure it's a file. Starting with Python 3.4, the
To check a directory, do:
To check whether a
You can also use
|
|||||||||||||||||||||
|
|
if you imported numpy already for other purposes then no need to import other libs like pathlib or os or paths etc.
|
|||
|
|
|
use this optimized code:- from os import path def ex(file):
|
||||
|
|
In Python 3.4 and on, use the new
So let's get a file that we know is a file:
By default,
If you dig into the implementation, though, you'll see that
Without a try statement:suppress Python 3.4 gives us the
Usage:
For earlier Pythons, you could roll your own
Easier with a try:
Other options that don't meet the ask for "without try":isfile
from the docs:
But if you examine the source of this function, you'll see it actually does use a try statement:
All it's doing is using the given path to see if it can get stats on it, catching If you intend to do something with the file, I would suggest directly attempting it with a try-except to avoid a race condition:
os.access Available for Unix and Windows is
It also suffers from the same race condition problems as
|
||||
|
|
|
|||||||||
|
|
||||
|
|
|
In Python 3.4 the language provides a new module to manage files:
|
||||
|
In 2016 the best way is still using
Or in Python 3 you can use
|
|||||||||
|
|
I'm the author of a package that's been around for about 10 years, and it has a function that addresses this question directly. Basically, if you are on a non-Windows system, it uses The code itself does not use a
And the doc…
The implementation, if you care to look, is here: https://github.com/uqfoundation/pox/blob/89f90fb308f285ca7a62eabe2c38acb87e89dad9/pox/shutils.py#L190 |
||||
|
|
In 2016, this is still arguably the easiest way to check if both a file exists and if it is a file:
Note: However, this approach will not lock the file in any way and therefore your code can become vulnerable to "time of check to time of use" (TOCTTOU) bugs. So raising exceptions is considered to be an acceptable, and Pythonic, approach for flow control in your program. And one should consider handling missing files with IOErrors, rather than |
||||
|
|
|
Although I always recommend using
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 |
||||
|
|
|
You can use the "OS" library of Python:
|
|||||||||||||||||||||
|
|
You can use the following open method to check if a file exists + readable:
|
|||||
|
|
You can write Brian's suggestion without the
|
||||
|
|
|
You can simply use the
|
|||||||||||||||||
|
|
You could try this (safer):
The ouput would be:
Then, depending on the result, your program can just keep running from there or you can code to stop it if you want. |
|||||||||
|
Importing For reference also see How to check whether a file exists using Python? If you need high-level operations, use |
|||||||||||||||||
|
|
I would try something along the lines of the following using the call to
|
|||||||||
|
|
Adding one more slight variation which isn't exactly reflected in the other answers. This will handle the case of the
Adding a variant based on suggestion from Shahbaz
Adding a variant based on suggestion from Peter Wood
|
|||||||||||||||||||||
|
|
You have the
This returns |
||||
|
|
|
Unlike
|
||||
|
|
|
Testing for files and folders with Assuming that the "path" is a valid path, this table shows what is returned by each function for files and folders: You can also test if a file is a certain type of file using
|
|||
|
|
|
You can use os.listdir to check if a file is in a certain directory.
|
|||||
|
|
Additionally,
Being |
||||
|
|
protected by NullPoiиteя Jun 10 '13 at 5:13
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
