[Tutor] os.getcwd() confusion

Peter Otten __peter__ at web.de
Mon May 20 09:21:47 CEST 2013


spangled spanner wrote:

> G'day,
> 
> I have a comprehension issue here! I have made two simple scripts:
> 
> ## script1
> 
> import os
> 
> print os.getcwd()
> 
> -----------------------------
> ## script 2
> 
> import os
> 
> f = open('test', 'wb')
> f.write(os.getcwd())
> f.close()
> 
> _________________
> 
> Both scripts are in my home directory.
> Using bash I cd to some other directory and call script 1:
>> python ../../script1.py
> and I get this result printed to the screen:
>> users/scriptdir/dir1/dir2
> i.e. the script returns the name of the directory that I'm currently
> working in.
> 
> However if I call script2:
>> python ../../script2.py
> I get a result that to me seems inconsistent: the script writes the
> directory
> that IT is in (i.e., NOT the name of the directory I'm currently working
> in),
> to the file 'test' in the home directory.
> 
> I don't quite understand what is happening here! Any explanation would be
> much appreciated.

The test file will be written in the current working directory, too. You are 
looking into a leftover test file from a previous run.



More information about the Tutor mailing list