繁体   English   中英

如何避免python在代码所在目录运行文件?

[英]How to avoid python running file in the directory where the code is located?

我正在编写一个脚本,该脚本将从 Internet 下载一个可执行文件,这将创建更多文件。 现在,如果我下载文件,它将被下载到我告诉它的目录中,但是当我使用 os.startfile() 打开它时,它会在 python 脚本所在的目录中创建文件。 我怎样才能避免这种情况?

在尝试运行可执行文件之前执行os.chdir()

os.chdir('<the target folder where you want the files to be created>')
...
os.startfile('<path to where the executable was downloaded>')

您可以将.py文件移动到任何其他目录。 或者您可以使用os.chdir()在运行时更改脚本的当前工作目录。

os.chdir('/path/to/directory')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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