【发布时间】:2014-01-14 18:56:58
【问题描述】:
我在桌面上创建了一个名为“headfirstpython”的文件夹,我需要将我当前的工作目录更改为该文件夹以及其中的子文件夹。我使用 os.getcwd() 来获取当前文件夹,它给了我“C\Python32”。我使用 os.chdir('../headfirstpython/chapter3') 来更改目录,但它告诉它找不到路径
>>> import os
>>> os.getcwd()
'C:\\Python32'
>>> os.chdir('../headfirstpython/chapter 3')
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
os.chdir('../headfirstpython/chapter 3')
WindowsError: [Error 3] The system cannot find the path specified: '../headfirstpython/chapter 3'
>>> os.chdir('../headfirstpython/chapter3')
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
os.chdir('../headfirstpython/chapter3')
WindowsError: [Error 3] The system cannot find the path specified: '../headfirstpython/chapter3'
>>>
【问题讨论】:
-
可能是斜线类型错误 (
/ instead of \\) 造成的? -
“桌面”上的文件夹不在根目录下,所以相对路径不起作用。尝试例如
"\\Users\\<your user name>\\Desktop\\the path"(或类似的,不记得确切的路径)。 -
你能像@JoachimPileborg 所说的那样尝试吗,我认为这会奏效。
标签: python