2016-11-29 110 views
-1

这里是我在Python编写代码:无效的文件打开方式“IO错误:[错误22]无效的模式(‘R’)或文件名:”

import os 
path=os.path.abspath("C:\Users\punagpal\Downloads\curl-7.50.3-win64-mingw\curl-7.50.3-win64-mingw\10.42.129.78_administrator_LogTasks.xml") 
f = open(path,'r') 
while True: 
    text = f.readline() 
    if 'name' in text: 
     print text 

但得到以下错误:

Invalid file open mode "IOError: [Errno 22] invalid mode ('r') or filename:" 
+0

@Selcuk,'path'不保留。我搞砸了被删除的评论。使用'path'作为变量是否是一个好习惯? –

+1

@JaiminAjmeri使用它作为变量名称没有什么问题,除非你做了一些类似的事情,比如'from os import path',这本身就是一种不好的编码习惯。 – Selcuk

回答

0

你需要逃避你的反斜杠(\)字符:

path=os.path.abspath("C:\\Users\\punagpal\\Downloads\\curl-7.50.3-win64-mingw\\curl-7.50.3-win64-mingw\\10.42.129.78_administrator_LogTasks.xml") 
相关问题