Domanda

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.

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top