我试图用python替换多个latex文档中的几行代码。这就是我所做的:
import fileinput, glob, string, sys, os
from os.path import join
def search_rep(path,search,replace):
# replace a string in multiple files
files = glob.glob(path)
for file in files:
if os.path.isfile(file):
for line in file.splitlines(): # or whatever arbitrary loop
if line.find(search) > -1:
print "Replacing" + replace + "on line: %s" % line
line.replace(search, replace)
def main():
path = "/home/stig/test/*.tex"
search = "/home/stig/hfag/oppgave/figs_plots/"
replace = "/home/stig/forskning_linux/oppgave_hf2/figs_plots/"
search_rep(path,search,replace)
if __name__ == "__main__":
sys.exit(main())
但是脚本不会改变文件中的任何内容。怎么了?在
谢谢
斯蒂格
考虑使用^{} module 。这很适合这个问题。Example:
另请参见
inplace
和backup
参数,这两个参数允许您就地进行替换(在发生错误时具有备份的安全性)。在相关问题 更多 >
编程相关推荐