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

115 views Asked by At

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?

2

There are 2 answers

0
Mike Vella On BEST ANSWER

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.

5
Aida Paul On

You are not trying to change to actual folder, but to "c:", proper command will be

import os 
os.chdir('c:\\')

And it will work just fine. The reason for double backslash is to escape the backslash (which works as escape character).