0

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.

r.v
  • 4,697
  • 6
  • 35
  • 57

1 Answers1

0

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.

r.v
  • 4,697
  • 6
  • 35
  • 57
  • Thanks, when I run it manually in the python shell at home it seems to work. So there must be some other issue in my script. the os.curdir command confused me a little bit. How do I get os.curdir to print the absolute path instead of "."? Also, I find this print>>f expression very handy, do you know the python 3 equivalent of it? EDIT: okay, I just saw the hint to use os.getcwd instead of os.curdir –  Mar 12 '13 at 21:55
  • [This](http://stackoverflow.com/a/9236236/567555) seems to answer your question about python 3. – r.v Mar 12 '13 at 22:00