How do I check if a file exists, using Python, without using a try:
statement?
|
||||
You can also use
if you need to be sure it's a file. |
|||||||||||||||||
|
You have the
|
|||||
|
Unlike
|
|||||||||
|
Use
|
|||||||||||||
|
|
|||||
|
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:
|
|||||||||||||||||||||
|
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.
|
|||||
|
|
||||
|
It doesn't seem like there's a meaningful functional difference between try/except and exists(), so you should use which one makes sense. If you want to read a file, if it exists, do
But if you just wanted to rename a file if it exists, and therefore don't need to open it, do
If you want to write to a file, if it doesn't exist, do
If you need file locking, that's a different matter. |
||||
|
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. |
|||||||||||||||||
|
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. |
|||||||||||||
|
Just to add to the confusion, it seems that the try: open() approach suggested above doesn't work in Python, as file access isn't exclusive, not even when writing to files, c.f. What is the best way to open a file for exclusive access in Python?. |
|||
|
Additionally, |
|||
|
You should definitely use this one.
|
|||||||||||||
|
This is helpful when checking for several files. Or you want to do a set intersection/ subtraction with an existing list. |
||||
|
You can simply use tempfile module to know whether file exists or not:
|
|||
If you want to do what in bash 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 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). 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. |
|||
|
You can write Brian's suggestion without the
|
|||
|
You can use following open method to check if file exists + readable
|
|||
|
This sample function will test for a file's presence in a very Pythonic way using try .. except:
|
||||
protected by NullPoiиteя Jun 10 '13 at 5:13
Thank you for your interest in this question.
Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.
Would you like to answer one of these unanswered questions instead?
IOError
, since it can be thrown for a number of reasons even if the file does exist - no rights, file locked, whatever. Exists != can be opened, let's not oversimplify. – Kos Jul 25 '12 at 15:30if exc.errno != errno.ENOENT: raise
. This will let the other errors through, so your catch block can handle just theIOError
s caused by a missing file. – num1 May 20 '13 at 3:19FileNotFoundError
exception in Python-3. – Honest Abe Feb 9 at 8:36