python文件路径双斜杠_在python中的Windows路径中出现双斜杠时出错(Error with double backslash in Windows path in Python)...

在python中的Windows路径中出现双斜杠时出错(Error with double backslash in Windows path in Python)

我想在python 3.3中使用Windows中的路径,但我有一个错误

FileNotFoundError: [Errno 2] No such file or directory: 'E:\\dir\\.project'

问题这个双斜线。 我用r读了解决方案。

def f(dir_from):

list_of_directory = os.listdir(dir_from)

for element in list_of_directory:

if os.path.isfile(os.path.join(dir_from, element)):

open(os.path.join(dir_from, element))

f(r'E:\\dir')

我又错了

FileNotFoundError: [Errno 2] No such file or directory: 'E:\\dir\\.project'

os.path.normpath(path)不解决我的问题。

我究竟做错了什么?

I want to work with paths in Windows in Python 3.3, but I have an error:

FileNotFoundError: [Errno 2] No such file or directory: 'E:\\dir\\.project'

The problem is the double backslash. I read the solution using r.

def f(dir_from):

list_of_directory = os.listdir(dir_from)

for element in list_of_directory:

if os.path.isfile(os.path.join(dir_from, element)):

open(os.path.join(dir_from, element))

f(r'E:\\dir')

I have this error again

FileNotFoundError: [Errno 2] No such file or directory: 'E:\\dir\\.project'

os.path.normpath(path) doesn't solve my problem.

What am I doing wrong?

原文:https://stackoverflow.com/questions/22567785

更新时间:2020-02-16 22:38

最满意答案

如果您使用的是原始字符串 ,则不需要转义反斜杠:

f(r'E:\dir')

当然,只需在路径中使用forwardslashes即可解决此问题(以及许多其他类似问题):

f('E:/dir')

If you are using a raw-string, then you do not escape backslashes:

f(r'E:\dir')

Of course, this problem (and many others like it) can be solved by simply using forwardslashes in paths:

f('E:/dir')

2018-09-19

相关问答

尝试将路径视为原始字符串文字,方法是在${EXECDIR}之前的引号前面加上“r”: ${firefox_binary}= Evaluate ....FirefoxBinary(r'${EXECDIR}${/}Firefox...')

这应该有效,因为机器人变量在字符串传递给python之前被替换,因此python解释器只能看到完整的字符串。 如果您不熟悉python原始字符串文字,请参阅以下问题: Python中“u”和“r”字符串标记到底是做什么的,原始字符串文字是什么? Try t

...

尝试使用 string destinationFile = System.IO.Path.Combine(msiDirectory, fileName).Replace(@"\\", @"\");

例: string path = "C:\Hg\temp\\LogFile.txt";

string output = path.Replace(@"\\", @"\");

output >>>

C:\Hg\temp\LogFile.txt

Try using string destinati

...

不要尝试使用字符串操作来建立文件路径。 Python有这样的os.path模块: import os.path

i = 3

print os.path.join("C:\Test", str(i) + ".jpg")

这将确保路径正确构建。 (另外,不要调用整型int ,因为它会影响内置的int()函数。) Don't try and use string manipulation to build up file paths. Python has the os.path module for

...

反斜杠通常用于转义特殊字符串。 例如: >>> print "hi\nbye"

hi

bye

告诉Python不要将斜线作为特殊字符,通常与使用“原始”字符串一样容易,可以通过在字符串前加字母'r'将其写成字符串字面值 。 >>> print r"hi\nbye"

hi\nbye

但是,即使是原始字符串,也不能以奇数个反斜杠结束。 这使得字符串拼接困难。 >>> print "hi" + r"\" + "bye"

File "", line 1

print "hi" + r"\"

...

\f被读作转义字符,类似于\n是换行符。 使用第二个反斜杠转义反斜杠: \\f变为\f 。 通常,您应始终转义反斜杠以避免此问题。 \f is being read as an escape character, similar to how \n is newline. Using a second backslash escapes the backslash: \\f becomes \f. In general, you should always escape backslashes t

...

如果您使用的是原始字符串 ,则不需要转义反斜杠: f(r'E:\dir')

当然,只需在路径中使用forwardslashes即可解决此问题(以及许多其他类似问题): f('E:/dir')

If you are using a raw-string, then you do not escape backslashes: f(r'E:\dir')

Of course, this problem (and many others like it) can be solved by simpl

...

使用4个反斜杠: Pattern.compile("((([a-zA-Z0-9])([a-zA-Z0-9 ]*)\\\\?)+)")

^^^^

你需要匹配一个反斜杠char: \ 。 反斜杠是正则表达式的一个特殊字符(用于预定义的类,例如\d ),需要使用另一个反斜杠: \\来转义。 由于Java使用字符串文字作为正则表达式,并且反斜杠也是字符串文字的特殊字符(例如,用于换行字符\n ),所以每个反斜杠

...

当您打印元组(或列表或许多其他类型的项)时,将打印所包含项的表示( repr() ),而不是字符串值。 对于更简单的类型,表示通常是您必须键入Python以获取值。 这使您可以更容易地区分容器中的项目与分隔它们的标点符号,还可以识别它们的类型。 (想想:是(1, 2, 3)三个整数的元组,还是字符串"1, 2"和整数3的元组 - 或其他一些值的组合?) 要查看任何字符串的repr() : print(repr(r'C:\hi'))

在交互式Python提示符下,只需指定任何值(或变量或表达式)即

...

更改参数列表以将路径编码为原始字符串: k = subprocess.Popen(['python', 'parser.py', '-f', r'C:\Report1\2011-03-14.txt'],

shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

一个读取文件并报告长度的简单程序: import sys

import os

userinput = sys.argv[1]

da

...

当您在String文字中对其进行硬编码时,只需要\\ 。 因为\是转义字符,所以它在运行时变为单个\ 。 您还可以使用/作为分隔符编写路径。 要清楚 String path = "C:\\Users\\chloe\\Documents";

创建一个值等于C:\Users\chloe\Documents的String (如果要print它)。 你也可以写 String path = System.getProperty("user.home") + File.separator + "Docume

...

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值