Skip to content

Instantly share code, notes, and snippets.

@DrDougPhD
Last active March 14, 2017 18:55
Show Gist options
  • Save DrDougPhD/be70ed85aa6fc2f96bfba911ecce9cac to your computer and use it in GitHub Desktop.
Save DrDougPhD/be70ed85aa6fc2f96bfba911ecce9cac to your computer and use it in GitHub Desktop.
This Python function is useful for argparse declarations that receive directories that both need to be verified existing and converted to absolute paths.
'''
Copyright 2017 Doug McGeehan under the GPLv3.
'''
import os
def verified_absolute_path(path):
abs_path = os.path.abspath(path)
assert os.path.isdir(abs_path),\
'No directory exists at "{}"'.format(abs_path)
return abs_path
# Demonstrate the script
if __name__ == '__main__':
current_dir = '.'
abs_path = verified_absolute_path(current_dir)
print('Converted {0} to {1}'.format(current_dir, abs_path))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment