How can I swap drives using python os?

I am trying to change the current directory from C:to Y: I have tried:

import os
os.chdir('Y:')

but I’m always mistaken in saying that he cannot find the disk. Essentially I'm looking for an equivalent

cd /d

in cmd.

+5
source share
2 answers

Are you sure that a Y:valid drive letter is valid?

Try os.chdir('C:')and make sure it works. (This works for me.)

+7
source

If it is a mapped network drive, it is best to use a UNC path instead of a mapped path. Also, when using paths under windows, try using the string modifier rif you are not using os.path.join.

import os
print os.getcwd()
os.chdir(r'\\server\path') 
print os.getcwd()
+1
source

All Articles