简体   繁体   中英

if user input is not a directory, how can I make the directory a default value

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?

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM