Skip to content

Instantly share code, notes, and snippets.

@llazzaro
Created April 11, 2017 21:16
Show Gist options
  • Save llazzaro/df6dfc2c3d45d1d7fb64851180a92229 to your computer and use it in GitHub Desktop.
Save llazzaro/df6dfc2c3d45d1d7fb64851180a92229 to your computer and use it in GitHub Desktop.
How to check that a file or directory exists with Python?
# example taken from https://tutorials.technology/tutorials/08-How-to-check-that-file-exists-with-Python.html
from pathlib import Path
def file_or_directory(pathname):
file_or_directory = Path(pathname)
if file_or_directroty.exists():
if file_or_directory.is_file():
print('Filename {0} is a file!'.format(pathname)
else:
print('Filename {0} is a directory!'.format(pathname)
else:
print('Filename {0} does not exists.'.format(pathname))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment