问题
I'm trying to change the current directory from C:
to Y:
I tried:
import os
os.chdir('Y:')
but I keep getting an error saying that it can't locate the drive. Essentially I'm looking for the equivalent of the
cd /d
command in cmd.
回答1:
Are you sure Y:
really is a valid drive letter?
Try os.chdir('C:')
and make sure that works. (It works for me.)
回答2:
If this is a mapped network drive, your best bet is to use the UNC path instead of the mapped path. Also, try to use a raw r
string modifier when using paths under windows, if you're not using os.path.join
.
import os
print os.getcwd()
os.chdir(r'\\server\path')
print os.getcwd()
来源:https://stackoverflow.com/questions/11057247/how-can-i-change-drives-using-python-os