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

pythonimport os

print(os.path.isfile(path))

Example

Let's check again whether the given path is a file:

pythonimport os

print(os.path.isfile('file.txt'))

The result of the executed code:

pythonTrue

Example

Now let's check if the given path is a file:

pythonimport os

print(os.path.isfile('dir'))

The result of the executed code:

pythonFalse

See also

  • method path.isdir of module os,
    which checks the existence of a folder
  • method path.exists of module os,
    which checks the existence of a path
  • method copy2 of module shutil,
    which copies a file with metadata
  • method rename of module os,
    which renames a file and folder
  • method path.join of module os,
    which unites the paths
  • method path.getsize of module os,
    which returns the size of the path in bytes