Home > Software design >  if user input is not a directory, how can I make the directory a default value
if user input is not a directory, how can I make the directory a default value

Time:07-07

Let us say a user input is an invalid directory. if so how can I make a script automatically set a default directory to a local file or the desktop?

CodePudding user response:

You can use

os.path.exists(path)

to see if it exists. If it does not, you can set set another path. If you want to change directory, you can use

os.chdir(path)

CodePudding user response:

You have to use the os library and check if a directory exists:

isFile = os.path.isdir(path) 

if isFile is false then set it to whatever default directory you want. If you want it to be set to Desktop, then use:

os.environ['USERPROFILE']   '\Desktop'

docs: https://docs.python.org/3/library/os.html#os.environ

  • Related