我正在尝试单独打开以下每个文件。
"C:\recipe\1,C:\recipe\2,C:\recipe\3,"
我尝试使用以下代码执行此操作:
import sys
import os
import re
line = "C:\recipe\1,C:\recipe\2,C:\recipe\3,"
line = line.replace('\\', '\\\\') # tried to escape control chars here
line = line.replace(',', ' ')
print line # should print "C:\recipe\1 C:\recipe\2 C:\recipe\3 "
for word in line.split():
fo = open(word, "r+")
# Do file stuff
fo.close()
print "\nDone\n"
当我运行它时,它给了我:
fo = open(单词," r +") IOError:[Errno 13]权限被拒绝:' C:'
所以它必须是原始字符串中'\r'
s未正确转义的结果。我尝试了许多其他方法来转义控制字符,但它们似乎都没有工作。我做错了什么?
答案 0 :(得分:2)
使用raw string:
line = r"C:\recipe\1,C:\recipe\2,C:\recipe\3,"
答案 1 :(得分:0)
如果由于某种原因你不使用原始字符串,你需要通过添加双斜杠来逃避单斜杠:
['C:\\recipe\\1', 'C:\\recipe\\2', 'C:\\recipe\\3', '']
输出:
data.notifyDataChanged()