Possible Duplicate:
Pythonic way to check if a file exists?
How to check if a file exists in python?
Scripting in Windows
isfile() Method to check if file exists. os. path. isfile() method in Python is used to check whether the specified path is an existing regular file or not.
We use the is_file() function, which is part of the Path class from the pathlib module, or exists() function, which is part of the os. path module, in order to check if a file exists or not in Python.
To check if a file exists, you pass the file path to the exists() function from the os.path standard library. If the file exists, the exists() function returns True . Otherwise, it returns False .
This is very easy using the os
module.
from os.path import exists
print exists("C:\somefile.txt")
Check the manual, this is a really easy question:
http://docs.python.org/library/os.path.html#os.path.exists
os.path.exists(path)
Return True if path refers to an existing path. Returns False for broken symbolic links. On some platforms, this function may return False if permission is not granted to execute os.stat() on the requested file, even if the path physically exists.
http://docs.python.org/library/os.path.html#os.path.exists
or if you want to also ensure that said path is actually a file (as opposed to a directory, link, etc):
http://docs.python.org/library/os.path.html#os.path.isfile
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With