As you have already found out, the difference between exists and isfile is the fact that the former returns True in case the given path is a directory or a file, while the latter only returns True if the path points to a file.
Directories and files are quite similar from the technical point of view. A file can contain any kind of data. A directory is just a special entry in the file system (at least on Unix operating systems it is just a special file) that represents the fact that it may contain files and other directories. It is a helpful means for building up a data structure. Using directories, you can organize your data in a hierarchical structure.
Especially in the Windows world, directories are often called "folders". I am sure that you yourself are using "folders" for organizing your files.
A path is an unambiguous pointer to a resource in the file system. It can either point to a file or to a directory.
Answer from Dr. Jan-Philip Gehrcke on Stack Overflowpython - Difference between os.path.exists and os.path.isfile? - Stack Overflow
python - How do I check whether a file exists without exceptions? - Stack Overflow
os.path.exists returning false, path 100% exists - Docker/Alpine (linux)
FIM – Linux framebuffer image viewer
As you have already found out, the difference between exists and isfile is the fact that the former returns True in case the given path is a directory or a file, while the latter only returns True if the path points to a file.
Directories and files are quite similar from the technical point of view. A file can contain any kind of data. A directory is just a special entry in the file system (at least on Unix operating systems it is just a special file) that represents the fact that it may contain files and other directories. It is a helpful means for building up a data structure. Using directories, you can organize your data in a hierarchical structure.
Especially in the Windows world, directories are often called "folders". I am sure that you yourself are using "folders" for organizing your files.
A path is an unambiguous pointer to a resource in the file system. It can either point to a file or to a directory.
A directory tests true for exists, but false for isfile. There are a few different types of filesystem objects for which that is also true.
isfile means that it is a regular file. It is more specific than exists.
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 if you need to be sure it's a file.
Return
Trueif 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)
pathlib
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(strict=True) in a try block:
try:
my_abs_path = my_file.resolve(strict=True)
except FileNotFoundError:
# doesn't exist
else:
# exists
Use os.path.exists to check both files and directories:
import os.path
os.path.exists(file_path)
Use os.path.isfile to check only files (note: follows symbolic links):
os.path.isfile(file_path)
I have a path I am using throughout my functions as a variable:
filepath = "/hold/some file.mkv"
When I plug it in to the script that loads in my container:
if os.path.exists(filepath):
print (filepath + ' exists')
else:
print (filepath + ' does not exist'I get:
"/hold/some file.mkv" does not exist
Which is odd because I am using filepath in other OS and Subprocess functions elsewhere in this same python file and those functions are working without issue.
However if I SSH into the container and run:
python
>>> Import OS
>>> if os.path.exists("/hold/some file.mkv"):
>>> print ('file exists')I get:
file exists
Same if I make "/hold/some file.mkv" a variable filepath, and call if os.path.exists(filepath):
What am I doing wrong here?