Skip to content

Instantly share code, notes, and snippets.

@jbranchaud
Created November 5, 2012 04:37
Show Gist options
  • Save jbranchaud/4015348 to your computer and use it in GitHub Desktop.
Save jbranchaud/4015348 to your computer and use it in GitHub Desktop.
A quick and tolerant directory validator for unix systems in Python
import os
"""
validate_directory: string -> string
given a string that represents a directory, this function will go through and
do some basic validation on it. If there is a problem with the directory as
given, it will be converted to the home directory and returned as is.
"""
def validate_directory(directory=None):
if directory == None or directory == '~' or not os.path.isdir(directory):
# if there is a problem, the default is the home directory
return os.path.expanduser('~')
return os.path.abspath(directory)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment