0

im relatively new in python community. the problem is that i changed the python directory(what i mean from this, is that where i save my work in python, its a folder in drive 'C') i want to change my directory to the other drive. i tried this:

import os


os.getcwd() 
>> 'C://python'
os.chcwd('E:/python')

it does change for the time being but if i close my python shell and check again, it gives my the same error and changes back to C...

Please help`

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • 2
    When you start the interactive shell, it'll automatically set the current working directory to whatever directory you were in when you started it. – TigerhawkT3 Jul 28 '15 at 05:32
  • Duplicate of: http://stackoverflow.com/questions/11057247/how-can-i-change-drives-using-python-os and http://stackoverflow.com/questions/6614323/os-getcwd-for-a-different-drive-in-windows – bufh Jul 28 '15 at 06:10

1 Answers1

0

Because each and every process in the system has its own current directory. So what happens here :

  • you have an interactive shell with a current directory
  • you start an interactive python shell : it receives the current directory of its parent
  • you change current directory for current python shell only
  • you use it until you close python
  • you close python and return in your interactive shell in original directory
  • you start python again always in original directory

Alternatively, as it looks that you are using Windows, you can start a Python shell from Windows explorer. Things are slightly different here :

  • if you directly click on the Python.exe file in its own directory, Windows explorer starts the program in this directory
  • if you use a shortcut to start the Python shell (or startup menu that only consist in shortcuts), the shortcut sets an initial directory. It it is not defined in shortcut properties, it is the directory containing the Python.exe file.

But here again, when you close the Python shell, its current dir is lost and next invocation will still follow above rules.

So what to do if you want to change the initial current directory :

  • the batch method (could be used on any system) : create a batch file that first change directory and then start Python interpreter with its full path (adapt paths to your actual needs)

    cd e:\Python
    c:\python\python.exe
    
  • use a shortcut and configure start directory in shortcut properties

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252