Python os.path.isfile() method Last Updated : 22 Mar, 2025 Comments Improve Suggest changes Like Article Like Report The os.path.isfile() method in Python is used to check if a path refers to a regular file (not a directory or any other type of file system object). This method can be particularly useful when you need to validate file paths before performing file operations like reading, writing, or modifying files. It returns a boolean value:True: If the path refers to a file that exists.False: If the path does not exist or is not a regular file.Example: Use of os.path.isfile() method This code demonstrates the use of the os.path.isfile() method in Python to check whether a given path is a file or not. Python import os path = '/home/User/Desktop/file.txt' isFile = os.path.isfile(path) print(isFile) path = '/home/User/Desktop/' isFile = os.path.isfile(path) print(isFile) OutputTrue False Explanation:If the file file.txt exists at the specified location, the first print statement will output True.The second print statement will output False because /home/User/Desktop/ is a directory, not a file.Syntax:os.path.isfile(path)Parameters:path: This is the path of the file you want to check. The path can be a relative or absolute path to the file.Return Value:The method returns:True if the path exists and is a regular file.False if the path does not exist or if it points to a directory or non-file object.Example of os.path.isfile() method1. Checking if a File Exists: Python import os # Example file path path = "example.txt" # Check if the file exists if os.path.isfile(path): print(f"{path} exists and is a regular file.") else: print(f"{path} does not exist or is not a file.") Outputexample.txt does not exist or is not a file. Explanation: The code checks if "example.txt" is a valid file. It prints whether the file exists and is a regular file or not.2. Checking for a Directory: Python import os # Example directory path path = "/home/user/documents" # Check if it's a file or not if os.path.isfile(path): print(f"{path} is a file.") else: print(f"{path} is not a file.") Output/home/user/documents is not a file. Explanation: The code checks if "/home/user/documents" is a valid file. It prints whether the path is a file or not. Comment More infoAdvertise with us Next Article Python os.path.isfile() method I ihritik Follow Improve Article Tags : Python python-os-module Practice Tags : python Similar Reads Python | os.path.basename() method OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name man 4 min read Python | os.path.commonpath() method OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is submodule of OS module in Python used for common path name mani 2 min read Python | os.path.commonprefix() method OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is submodule of OS module in Python used for common path name mani 2 min read Python | os.path.dirname() method OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name man 2 min read Python | os.path.exists() method os.path.exists() method in Python is used to check whether the specified path exists or not. This method can be also used to check whether the given path refers to an open file descriptor or not. os.path.exists() Syntax in PythonSyntax: os.path.exists(path) Parameter: path: A path-like object repres 2 min read Python | os.path.lexists() method OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in python used for common path name man 2 min read Python | os.path.expanduser() method OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common pathname mani 4 min read Python | os.path.expandvars() method OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path the module is a submodule of OS module in Python used for common pathname 3 min read Python | os.path.getatime() method OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name man 2 min read Python | os.path.getmtime() method OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name man 2 min read Like