如何打开文件并更改行
How to open a file and change a line
我查看了很多关于此的主题,但找不到正确的答案:
我对 python 有点陌生。我在 python 中打开一个文件,在开始行之前插入一行,在最后一行之前插入一行,然后在末尾将所有行重建为一个字符串变量。
这是我试图在打开的文件上 运行 的函数。这会将 mode="0" 的所有行更改为 mode="1",反之亦然。
def Modes(file, mode):
endstring = ''
if(mode == 1):
mode0 = 'mode="0"'
mode1 = 'mode="1"'
else:
mode0 = 'mode="1"'
mode1 = 'mode="0"'
for line in iter(file):
if 'Modes' in line:
line = line.replace(mode0,mode1)
endstring += line
return endstring
所以我尝试以下操作:
mode = 1
input_file = open("c:\myfile.txt")
input_file.readlines()
lengthlines = len(input_file)
#insert line at position 1
input_file.insert(1,'<VARIABLE name="init1" />')
#insert line at last line position - 1
input_file.insert(lengthlines,'<VARIABLE name="init2" />')
#join the lines back up again
input_file = "".join(input_file)
#run the modes function - to replace all occurrences of mode=x
finalfile = Modes(input_file,mode)
print finalfile
然后我收到错误 "object of type file, has no "len()"" 和一般 object/list 错误。
我好像把 objects/lists 等搞混了,但我不确定在哪里 - 如果有任何帮助,我们将不胜感激 - 干杯
input_file.readlines()
returns 内容但没有分配给 input_file。
您必须将调用的 return 值分配给变量,如下所示:
file_content = input_file.readlines()
然后将其传递给 len()
lengthlines = len(file_content)
编辑:使用 len() 解决问题会导致更多异常。
这应该大致做你想做的事:
mode = 1
with open("c:\myfile.txt") as input_file:
file_content = list(input_file.readlines())
file_content.insert(0,'<VARIABLE name="init1" />')
file_content.append('<VARIABLE name="init2" />')
finalfile = Modes(file_content,mode)
print finalfile
如果您想坚持多行,您可能必须在函数中更改字符串连接。
endstring += line + '\n'
return endstring.rstrip('\n')
虽然这还没有将新内容写回文件。
EDIT2:完成文件后关闭文件总是好的做法,因此我更新了上面的内容以使用处理此问题的上下文管理器。您也可以在完成后显式调用 input_file.close()
。
我查看了很多关于此的主题,但找不到正确的答案:
我对 python 有点陌生。我在 python 中打开一个文件,在开始行之前插入一行,在最后一行之前插入一行,然后在末尾将所有行重建为一个字符串变量。
这是我试图在打开的文件上 运行 的函数。这会将 mode="0" 的所有行更改为 mode="1",反之亦然。
def Modes(file, mode):
endstring = ''
if(mode == 1):
mode0 = 'mode="0"'
mode1 = 'mode="1"'
else:
mode0 = 'mode="1"'
mode1 = 'mode="0"'
for line in iter(file):
if 'Modes' in line:
line = line.replace(mode0,mode1)
endstring += line
return endstring
所以我尝试以下操作:
mode = 1
input_file = open("c:\myfile.txt")
input_file.readlines()
lengthlines = len(input_file)
#insert line at position 1
input_file.insert(1,'<VARIABLE name="init1" />')
#insert line at last line position - 1
input_file.insert(lengthlines,'<VARIABLE name="init2" />')
#join the lines back up again
input_file = "".join(input_file)
#run the modes function - to replace all occurrences of mode=x
finalfile = Modes(input_file,mode)
print finalfile
然后我收到错误 "object of type file, has no "len()"" 和一般 object/list 错误。
我好像把 objects/lists 等搞混了,但我不确定在哪里 - 如果有任何帮助,我们将不胜感激 - 干杯
input_file.readlines()
returns 内容但没有分配给 input_file。
您必须将调用的 return 值分配给变量,如下所示:
file_content = input_file.readlines()
然后将其传递给 len()
lengthlines = len(file_content)
编辑:使用 len() 解决问题会导致更多异常。 这应该大致做你想做的事:
mode = 1
with open("c:\myfile.txt") as input_file:
file_content = list(input_file.readlines())
file_content.insert(0,'<VARIABLE name="init1" />')
file_content.append('<VARIABLE name="init2" />')
finalfile = Modes(file_content,mode)
print finalfile
如果您想坚持多行,您可能必须在函数中更改字符串连接。
endstring += line + '\n'
return endstring.rstrip('\n')
虽然这还没有将新内容写回文件。
EDIT2:完成文件后关闭文件总是好的做法,因此我更新了上面的内容以使用处理此问题的上下文管理器。您也可以在完成后显式调用 input_file.close()
。