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.
Why doesn't 'C:' mean what I think it means?
Question
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
OTHER TIPS
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).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow