如果用户输入不是目录,则如何使该目录成为默认值

发布于 2025-02-13 14:03:14 字数 52 浏览 0 评论 0原文

让我们说用户输入是无效目录。如果是这样,我该如何使脚本自动将默认目录设置为本地文件或桌面?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜味超标? 2025-02-20 14:03:14

您必须使用OS库,并检查目录是否存在:

isFile = os.path.isdir(path) 

如果ISFILE为false,则将其设置为您想要的任何默认目录。如果您希望将其设置为桌面,请使用:

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

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

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

娇纵 2025-02-20 14:03:14

您可以使用

os.path.exists(path)

它是否存在。如果没有,则可以设置另一个路径
如果要更改目录,则可以使用

os.chdir(path)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文