Skip to content

Instantly share code, notes, and snippets.

@llazzaro
Created April 11, 2017 21:16
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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