【发布时间】:2018-10-01 14:34:23
【问题描述】:
Given 是一个包含 windows 文件路径的变量。然后我必须去阅读这个文件。这里的问题是路径包含转义字符,我似乎无法摆脱它。我检查了 os.path 和 pathlib,但都已经期望正确的文本格式,我似乎无法构造。
例如这个。请注意,给出了 fPath,所以我不能在它的前面加上 r 作为原始路径。
#this is given, I cant rawpath it with r
fPath = "P:\python\t\temp.txt"
file = open(fPath, "r")
for line in file:
print (line)
如何通过以下函数或方法打开 fPath:
"P:\python\t\temp.txt"
到
"P:/python/t/temp.txt"
我试过也试过 .replace("\","/"),还是不行。
我为此使用 Python 3.7。
【问题讨论】:
-
"P:\python\t\temp.txt".replace("\\", "/") 适合我!
-
我使用了替换(“\\\”,“/”)。在这里写的时候甚至被转义了。
-
@Fourier,对我来说不是。这就是我得到的:
OSError: [Errno 22] Invalid argument: 'P:/python\t\temp.txt' -
能否粘贴 temp.txt 属性中的路径
-
@AlbinPaul 如果我明白你在问什么:文件的属性窗口列出了这个
Location: P:\python\t
标签: python python-3.x