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'
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.