import os
# Check using os.path.isdir
os.path.isdir("DIRECTORY_PATH")
# Check using os.path.exists
os.path.exists("DIRECTORY_PATH")
We are using the os module of Python here to check if a directory exists on the system or not. In the above code snippet:
You can make a directory path as below
directory_path = os.path.join(os.getcwd(), 'folder_name')
from pathlib import Path
# Check for a folder
Path('folder_name').is_dir()
# Check for a file
(Path.cwd() / 'folder_name' / 'file_name.txt').exists()
import os
dir_path = '/usr/bin/folder_name'
os.path.is_dir(dir_path)
import os
directory_path = '/usr/folder_name'
os.path.exists(directory_path)
0 Comments