Why doesn't Python remember the initial directory?

Giacomo Alzetta giacomo.alzetta at gmail.com
Sun Aug 19 17:01:15 EDT 2012


Il giorno domenica 19 agosto 2012 22:42:16 UTC+2, kj ha scritto:
> As far as I've been able to determine, Python does not remember
> 
> (immutably, that is) the working directory at the program's start-up,
> 
> or, if it does, it does not officially expose this information.
> 
> 
> 
> Does anyone know why this is?  Is there a PEP stating the rationale
> 
> for it?
> 
> 
> 
> Thanks!

You can obtain the working directory with os.getcwd().

giacomo at jack-laptop:~$ echo 'import os; print os.getcwd()' > testing-dir.py
giacomo at jack-laptop:~$ python testing-dir.py 
/home/giacomo
giacomo at jack-laptop:~$ cd Documenti
giacomo at jack-laptop:~/Documenti$ python ../testing-dir.py 
/home/giacomo/Documenti
giacomo at jack-laptop:~/Documenti$ 

Obviously using os.chdir() will change the working directory, and the os.getcwd() will not be the start-up working directory, but if you need the start-up working directory you can get it at start-up and save it in some constant.



More information about the Python-list mailing list