python os.chdir 正在修改传入的目录名 [英] Python os.chdir is modifying the passed directory name
问题描述
我正在尝试使用 os.chdir 更改 python 中的当前工作目录.我有以下代码:
I am trying to change the current working directory in python using os.chdir. I have the following code:
import os
os.chdir("C:UsersJoshDesktop20130216")
然而,当我运行它时,它似乎改变了目录,因为它出现了以下错误消息:
However, when I run it, it seems to change the directory, as it comes out with the following error message:
Traceback (most recent call last):
File "C:UsersJoshDesktopLapseBot 1.0LapseBot.py", line 3, in <module>
os.chdir("C:UsersJoshDesktop20130216")
WindowsError: [Error 2] The system cannot find the file specified
'C:\Users\Josh\Desktopx8130216'
有人可以帮我吗?
推荐答案
Python 将路径的 2013
部分解释为 转义序列 201
,它映射到字符 x81
,即 ü(当然,C:UsersJoshDesktopü30216
不存在).
Python is interpreting the 2013
part of the path as the escape sequence 201
, which maps to the character x81
, which is ü (and of course, C:UsersJoshDesktopü30216
doesn't exist).
使用原始字符串,以确保 Python 不会尝试将 后面的任何内容解释为转义序列.
Use a raw string, to make sure that Python doesn't try to interpret anything following a as an escape sequence.
os.chdir(r"C:UsersJoshDesktop20130216")
这篇关于python os.chdir 正在修改传入的目录名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!