Windows路径中Python中的双反斜杠错误 [英] Error with double backslash in Windows path in Python
问题描述
我想在Windows 3.3中使用Windows中的路径,但是出现错误:
I want to work with paths in Windows in Python 3.3, but I have an error:
FileNotFoundError:[错误2]没有这样的文件或目录: 'E:\\ dir \\.project'
FileNotFoundError: [Errno 2] No such file or directory: 'E:\\dir\\.project'
问题是双反斜杠.我使用r阅读解决方案.
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')
我再次遇到此错误
FileNotFoundError:[错误2]没有这样的文件或目录: 'E:\\ dir \\.project'
FileNotFoundError: [Errno 2] No such file or directory: 'E:\\dir\\.project'
os.path.normpath(path)
不能解决我的问题.
我在做什么错了?
推荐答案
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')
这篇关于Windows路径中Python中的双反斜杠错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!