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

【问题】

默认打开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:

9A8B8BF501A38A36!1673?filename=os_module_chdir[1].png

然后试了试,果然可以了:>>> os.chdir("d:\");

SyntaxError: EOL while scanning string literal

>>> os.chdir("d:\\");

>>> print os.getcwd();

d:\

>>>

【总结】

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值