Created
April 11, 2017 21:16
-
-
Save llazzaro/df6dfc2c3d45d1d7fb64851180a92229 to your computer and use it in GitHub Desktop.
How to check that a file or directory exists with Python?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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