【转载】路径双反斜杠!!!Python IDLE或shell中切换路径

Python IDLE或shell中切换路径
在Python自带的编辑器IDLE中或者python shell中不能使用cd命令,那么跳到目标路径呢。方法是使用os包下的相关函数实现路径切换功能。

import os
os.getcwd() #获取当前路径
os.chdir("D:\\test") #跳到目标路径下
os.chdir('D:\\test') #单引号、双引号都可以

>>> os.chdir("C:\\python37\2019pythonshel37\diedai")
Traceback (most recent call last):
File "<pyshell#56>", line 1, in <module>
os.chdir("C:\\python37\2019pythonshel37\diedai")
FileNotFoundError: [WinError 3] 系统找不到指定的路径。: 'C:\\python37\x819pythonshel37\\diedai'
>>> os.chdir("C:\\python37\\2019pythonshel37\\diedai")

猜你喜欢

转载自www.cnblogs.com/xuanbjut/p/10701921.html
今日推荐