duglus
answered Aug 16 '21 00:00
is_file()
is_file() will check wheather file is exists at given Path object or not.
returns : if file exists is_file() return True , if file dont exists is_file() will returns False
is_file() is invoked from the Path object
following is the step to check if a file exists in Python :
1. import Path from pathlib . because Path object is needed later
from pathlib import Path
2. provide the absolute path to create Path object
p = Path(Provide the complete path to the file)
3. invoke is_file() function to check the file exists or not
p.is_file()
it returns True if a file exists, it returns false if a file doesn't exist.
Final code :
from pathlib import Path
p = Path('testfolder/test.py')
print(p.is_file())