Search code examples
pythonwindowspathdirectorygetcwd

Why doesn't 'C:' mean what I think it means?


On Windows 7 I fire up my IDLE Python 2.7.5 Shell:

>>> import os
>>> os.getcwd()
'C:\\Python27'
>>> os.path.relpath('C:\\')
'..'
>>> os.path.relpath('C:')
'.'
>>> os.chdir('C:')
>>> os.getcwd()
'C:\\Python27'

What is going on, and why does it have to be this complicated?


Solution

  • On Windows the behaviour can be a bit strange - it behaves differently if you start Python from cmd.exe or if you start it directly (not going through cmd.exe). As has been pointed out the correct command is os.chdir('c:\\'). this answer provides more detail.