How To Check If a File is a Directory or Regular File in Python
How To Check If a File is a Directory or Regular File in Python
to check a file is a normal regular file or directory or a device file in python we have to import os module. with python os module we can find the given path is a file or directory.
Check If a File is a Directory or Regular File in Python
Example 1:
import os path=r'C:\Users\devops\AppData\Local\Programs\Python\Python37' if os.path.isdir(path): print("It is a directory") elif os.path.isfile(path): print("It is a normal file") else: print("It is a device file " )
os.path.isidr() function is used to check given path is a directory or not.
os.path.isfile() function is used to check given path is file or not.
Output:
It is a directory Process finished with exit code 0
Example 2:
import os path=r'C:\Users\enaknar\Desktop\pycharm\fsg.py' if os.path.isdir(path): print("It is a directory") elif os.path.isfile(path): print("It is a normal file") else: print("It is a device file " )
Output:
It is a normal file Process finished with exit code 0
- Check if a file path is a file or a directory
- How To Check If a File is a Directory or Regular File in Python