
๐ Python Check if File Exists
Python Check if File Exists: In Python, verifying the existence of a file or directory is crucial for handling files efficiently. Python offers several built-in methods to check file presence using the os module and pathlib module. Let’s explore these methods with examples.
๐1. Check File or Directory with os.path.exists()
The os.path.exists() method is a simple way to check if a file or directory exists. It returns True if the specified path exists; otherwise, it returns False.
๐Python Check if File Exists Example Code:
python
import os.path
from os import path
def main():
print(“File exists:”, path.exists(‘Software_Moji_Moji.txt’))
print(“File exists:”, path.exists(‘career_Software_Moji_Moji.txt’))
print(“Directory exists:”, path.exists(‘myDirectory’))
if __name__ == “__main__”:
main()
๐Python Check if File Exists Output:
sql
File exists: True
File exists: False
Directory exists: False
๐2. Verify File Only with os.path.isfile()
To confirm if a path points specifically to a file, use the os.path.isfile() method.
๐Example Code:
python
import os.path
from os import path
def main():
print(“Is it a File?”, path.isfile(‘Software_Moji_Moji.txt’))
print(“Is it a File?”, path.isfile(‘myDirectory’))
if __name__ == “__main__”:
main()
๐Output:
vbnet
Is it a File? True
Is it a File? False
๐3. Verify Directory Only with os.path.isdir()
To check if a path is specifically a directory, use os.path.isdir() method.
๐Example Code:
python
import os.path
from os import path
def main():
print(“Is it Directory?”, path.isdir(‘Software_Moji_Moji.txt’))
print(“Is it Directory?”, path.isdir(‘myDirectory’))
if __name__ == “__main__”:
main()
๐Output:
vbnet
Is it Directory? False
Is it Directory? True
๐4. Using pathlib.Path.exists() for Python 3.4+
For Python 3.4 and newer versions, the pathlib module offers a cleaner way to check file existence.
๐Example Code:
python
import pathlib
file = pathlib.Path(“Software_Moji_Moji.txt”)
if file.exists():
print(“File exists”)
else:
print(“File does not exist”)
๐Output:
arduino
File exists
๐5. Complete Code Example for File and Directory Verification
This comprehensive example combines multiple methods to check file presence, type, and directory existence.
๐Example Code:
python
import os
from os import path
def main():
print(“Operating System:”, os.name)
print(“Item exists:”, path.exists(“Software_Moji_Moji.txt”))
print(“Item is a file:”, path.isfile(“Software_Moji_Moji.txt”))
print(“Item is a directory:”, path.isdir(“Software_Moji_Moji.txt”))
if __name__ == “__main__”:
main()
๐Output:
yaml
Operating System: posix
Item exists: True
Item is a file: True
Item is a directory: False
๐Python Check if File Exists Summary
โ
os.path.exists() โ Returns True if the path (file or directory) exists.
โ
os.path.isfile() โ Returns True only if the path is a file.
โ
os.path.isdir() โ Returns True only if the path is a directory.
โ
pathlib.Path.exists() โ Preferred for Python 3.4+ for checking file or directory existence.