π 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.
