我有以下文件,我想替换#sys.path.insert(0,os.path.abspath('。')) 使用 sys.path.extend([' path1',' path2'])
import sys
import os
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#sys.path.insert(0, os.path.abspath('.'))
# -- General configuration ------------------------------------------------
但是,以下代码不会更改该行。
with open(os.path.join(conf_py_path, "conf.py"), 'r+') as cnfpy:
for line in cnfpy:
line.replace("#sys.path.insert(0, os.path.abspath('.')))",
"sys.path.extend(%s)\n" %src_paths)
cnfpy.write(line)
如何更换生产线?
答案 0 :(得分:1)
尝试fileinput在文件中就地更改字符串:
import fileinput
for line in fileinput.input(filename, inplace=True):
print(line.replace(string_to_replace, new_string))