Python os.chdir正在修改传递的目录名称 [英] Python os.chdir is modifying the passed directory name
问题描述
我正在使用os.chdir更改python中的当前工作目录。我有以下代码:
import os
os.chdir(C:\Users \Josh\Desktop\20130216)
但是,当我运行它,似乎更改目录,因为它出现了以下错误消息:
追溯(最近的最后一次呼叫):
文件C:\Users\Josh\Desktop\LapseBot 1.0\LapseBot.py,第3行在< module>
os.chdir(C:\Users\Josh\Desktop\20130216)
WindowsError:[错误2]系统找不到指定的文件
'C:\\ \\\Users\\Josh\\Desktop\x8130216'
任何人都可以帮忙我正在解释 \2013
的部分内容。路径作为转义序列 \201
,映射到角色 \x81
,这是ü(当然, C:\Users\Josh\Desktopü30216
不存在)。
使用原始字符串,确保Python不会尝试将 \
之后的任何内容解释为转义序列。
os.chdir(rC:\Users\Josh\Desktop\20130216)
I am trying to change the current working directory in python using os.chdir. I have the following code:
import os
os.chdir("C:\Users\Josh\Desktop\20130216")
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:\Users\Josh\Desktop\LapseBot 1.0\LapseBot.py", line 3, in <module>
os.chdir("C:\Users\Josh\Desktop\20130216")
WindowsError: [Error 2] The system cannot find the file specified
'C:\\Users\\Josh\\Desktop\x8130216'
Can anyone help me?
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:\Users\Josh\Desktopü30216
doesn't exist).
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:\Users\Josh\Desktop\20130216")
这篇关于Python os.chdir正在修改传递的目录名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!