The path.isfile method of the os module
The path.isfile method checks whether the file specified in the method parameter exists. This method can also be used to determine whether the path specified in the parameter is a file. The method returns a boolean value: if the path is a file, then True, otherwise - False.
Syntax
import os
print(os.path.isfile(path))
Example
Let's check again whether the given path is a file:
import os
print(os.path.isfile('file.txt'))
The result of the executed code:
True
Example
Now let's check if the given path is a file:
import os
print(os.path.isfile('dir'))
The result of the executed code:
False
See also
-
method
path.isdirof moduleos,
which checks the existence of a folder -
method
path.existsof moduleos,
which checks the existence of a path -
method
copy2of moduleshutil,
which copies a file with metadata -
method
renameof moduleos,
which renames a file and folder -
method
path.joinof moduleos,
which unites the paths -
method
path.getsizeof moduleos,
which returns the size of the path in bytes