I am running python code where I want to write some output to a particular folder (which different from the location where I execute the script).

Therefore I was planning to change the path of Python to this particular folder using the os module:

os.chdir("myLocation.../Folder")

However, the script still writes to the folder where I executed the script, and when I invoke the command

os.curdir

it returns ".".

I am a little bit lost here and would appreciate any hint.

有帮助吗?

解决方案

os.chdir should do the correct thing. Here is some code used for testing on python REPL, assuming you have a ./test dir in working dir.

>>> import os
>>> os.chdir('test')
>>> f = open('testfile', 'w')
>>> print>>f, 'hello world'
>>> f.close()

test/testfile is now present with the right contents.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top