【问题】
默认打开Python Shell,当前目录是在其安装路径下的:
比如这这里的:
D:\tmp\WordPress\DevRoot\Python27
现在想要切换当前工作目录
【解决过程】
1.网上找了下,参考:
结果都无法切换目录,出错如下:Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> print os.getcwd()
Traceback (most recent call last):
File "", line 1, in
print os.getcwd()
NameError: name 'os' is not defined
>>> import os
>>> print os.getcwd()
D:\tmp\WordPress\DevRoot\Python27
>>> os.chrdir("D:\")
SyntaxError: EOL while scanning string literal
>>> os.chrdir("..")
Traceback (most recent call last):
File "", line 1, in
os.chrdir("..")
AttributeError: 'module' object has no attribute 'chrdir'
>>> os.chrdir("C:\");
SyntaxError: EOL while scanning string literal
>>> os.chrdir("D:\\");
Traceback (most recent call last):
File "", line 1, in
os.chrdir("D:\\");
AttributeError: 'module' object has no attribute 'chrdir'
2.后来实在不行,自己去打开Python 2.7自带的手册,去看了看os到底有哪些函数,然后就发现,原来os有此函数的,但是名字是chdir不是chrdir:
然后试了试,果然可以了:>>> os.chdir("d:\");
SyntaxError: EOL while scanning string literal
>>> os.chdir("d:\\");
>>> print os.getcwd();
d:\
>>>
【总结】
上面的帖子,原创者估计是手误,把chdir写成chrdir,但是转帖者,貌似也没有自己去验证过。
导致此处试了半天,都不行,最后还是靠查阅官方资料,解决了问题。
看来,还是官方资料,来的靠谱,国内的帖子,都还是垃圾比较多。尤其是转帖且不验证者,太挫了。。