Skip to content

Instantly share code, notes, and snippets.

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
What would you like to do?
How to check that a file or directory exists with Python?
# example taken from
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)
print('Filename {0} is a directory!'.format(pathname)
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