如何逐行replace(更新)文件中的文本

我试图通过阅读每一行来replace文本文件,testing它,然后写入,如果它需要更新。 我不想保存为新文件,因为我的脚本已经先备份文件并在备份上运行。

这是我到目前为止…我从os.walk()得到fpath,我保证pathmatch var返回正确:

fpath = os.path.join(thisdir, filename) with open(fpath, 'r+') as f: for line in f.readlines(): if '<a href="' in line: for test in filelist: pathmatch = file_match(line, test) if pathmatch is not None: repstring = filelist[test] + pathmatch print 'old line:', line line = line.replace(test, repstring) print 'new line:', line f.write(line) 

但是最终发生的是,我只修改了几行(正确更新,介意你,但是从前面的文件重复)。 我认为这是一个范围界定问题。

*另外:我想知道如何只replace比赛的第一个实例上的文本,例如,我不想匹配显示文本,只有底层的href。

首先,你要写行是否符合模式。 否则,你只写出匹配的行。

其次,在读取行和写入结果之间,需要截断文件(可以是f.seek(0)然后是f.truncate() ),或者closures原始文件并重新打开。 select前者,我会得到类似的结果:

 fpath = os.path.join(thisdir, filename) with open(fpath, 'r+') as f: lines = f.readlines() f.seek(0) f.truncate() for line in lines: if '<a href="' in line: for test in filelist: pathmatch = file_match(line, test) if pathmatch is not None: repstring = filelist[test] + pathmatch line = line.replace(test, repstring) f.write(line) 
  1. 打开文件进行读取并将所有行复制到内存中。 closures文件。
  2. 将内存中的行应用您的转换。
  3. 打开文件进行写入,写出内存中的所有文本行。

 with open(filename, "r") as f: lines = (line.rstrip() for line in f) altered_lines = [some_func(line) if regex.match(line) else line for line in lines] with open(filename, "w") as f: f.write('\n'.join(altered_lines) + '\n') 

一种(相对)安全的方式来replace文件中的一行。

 #!/usr/bin/python # defensive programming style # function to replace a line in a file # and not destroy data in case of error def replace_line(filepath, oldline, newline ): """ replace a line in a temporary file, then copy it over into the original file if everything goes well """ # quick parameter checks assert os.exists(filepath) # ! assert ( oldline and str(oldline) ) # is not empty and is a string assert ( newline and str(newline) ) replaced = False written = False try: with open(filepath, 'r+') as f: # open for read/write -- alias to f lines = f.readlines() # get all lines in file if oldline not in lines: pass # line not found in file, do nothing else: tmpfile = NamedTemporaryFile(delete=True) # temp file opened for writing for line in lines: # process each line if line == oldline: # find the line we want tmpfile.write(newline) # replace it replaced = True else: tmpfile.write(oldline) # write old line unchanged if replaced: # overwrite the original file f.seek(0) # beginning of file f.truncate() # empties out original file for tmplines in tmpfile: f.write(tmplines) # writes each line to original file written = True tmpfile.close() # tmpfile auto deleted f.close() # we opened it , we close it except IOError, ioe: # if something bad happened. printf ("ERROR" , ioe) f.close() return False return replaced and written # replacement happened with no errors = True 

(注意:这只replace整个行,以及文件中匹配的所有行)