我总是在模式 </IfModule>
之前附加新行。
我怎样才能用 Python 完成这个任务。
仅供引用,我的文件不是使用 lxml/元素树的 XML/HTML。 IfModule
是我的 .htaccess
的一部分文件
我的想法是反转文件并搜索模式,如果找到则在其后面添加我的行。不太确定如何继续。
最佳答案
通读文件,当你找到你应该输出的行时,先输出一些内容,然后输出原始行。
with open('.htaccess') as fin, open('.htaccess-new', 'w') as fout:
for line in fin:
if line.strip() == '</IfModule>':
fout.write('some stuff before the line\n')
fout.write(line)
就地更新文件:
import fileinput
for line in fileinput.input('.htaccess', inplace=True):
if line.strip() == '</IfModule>':
print 'some stuff before the line'
print line,
关于Python:如何在我的匹配模式之前添加一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16855561/