问题
I'm writing a script that will download an executable from the internet, which will create more files. Now, if I download the file, it will be downloaded to the directory I told it, but when I open it using os.startfile(), it creates the files to the directory where the python script is located. How can I avoid that?
回答1:
Either you can move the .py
file to any other directory.
or
You can use os.chdir() to change the current working directory of the script during the runtime.
os.chdir('/path/to/directory')
回答2:
Do a os.chdir()
before trying to run the executable:
os.chdir('<the target folder where you want the files to be created>')
...
os.startfile('<path to where the executable was downloaded>')
来源:https://stackoverflow.com/questions/64506537/how-to-avoid-python-running-file-in-the-directory-where-the-code-is-located