How can I change drives using python os?

好久不见. 提交于 2019-12-22 05:23:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!