最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Python Shell中切换当前工作目录(路径)

Python crifan 11820浏览 0评论

【问题】

默认打开Python Shell,当前目录是在其安装路径下的:

比如这这里的:

D:\tmp\WordPress\DevRoot\Python27

现在想要切换当前工作目录

【解决过程】

1.网上找了下,参考:

python shell中改变当前工作路径

python shell中改变当前工作路径

结果都无法切换目录,出错如下:

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 "<pyshell#0>", line 1, in <module>
    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 "<pyshell#4>", line 1, in <module>
    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 "<pyshell#6>", line 1, in <module>
    os.chrdir("D:\\");
AttributeError: 'module' object has no attribute 'chrdir'

2.后来实在不行,自己去打开Python 2.7自带的手册,去看了看os到底有哪些函数,然后就发现,原来os有此函数的,但是名字是chdir不是chrdir

os_module_chdir

然后试了试,果然可以了:

>>> os.chdir("d:\");
	 
SyntaxError: EOL while scanning string literal
>>> os.chdir("d:\\");
>>> print os.getcwd();
d:\
>>> 

【总结】

上面的帖子,原创者估计是手误,把chdir写成chrdir,但是转帖者,貌似也没有自己去验证过。

导致此处试了半天,都不行,最后还是靠查阅官方资料,解决了问题。

看来,还是官方资料,来的靠谱,国内的帖子,都还是垃圾比较多。尤其是转帖且不验证者,太挫了。。

转载请注明:在路上 » 【已解决】Python Shell中切换当前工作目录(路径)

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (1)

  1. 靠 原来如此
    vincent7年前 (2017-03-28)回复
85 queries in 0.177 seconds, using 22.19MB memory