Skip to content

Instantly share code, notes, and snippets.

@leavlzi
Last active November 1, 2019 19:19
Show Gist options
  • Save leavlzi/28bcb94b4e5fec9681cde1b5defb150f to your computer and use it in GitHub Desktop.
Save leavlzi/28bcb94b4e5fec9681cde1b5defb150f to your computer and use it in GitHub Desktop.
How to check if a file exists use new Pathlib
from pathlib import Path
fname = Path("c:\\test\\abc.txt")
print(fname.exists()) # true
print(fname.is_file()) # true
print(fname.is_dir()) # false
dir = Path("c:\\test\\")
print(dir.exists()) # true
print(dir.is_file()) # false
print(dir.is_dir()) # true
# If check
fname = Path("c:\\test\\abc.txt")
if fname.is_file():
print("file exist!")
else:
print("no such file!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment