![](/img/trans.png)
[英]how to open a file by passing a variable name which contains the directory where the file is located in 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.