python import os.chdir,Python os.chdir正在修改传递的目录名称

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

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")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值