1
我想从另一个python文件中替换/修改python文件中的一部分字符串。使用另一个python文件修改/替换python文件中的一行
我想在其他PY更换该生产线是:
a.setSystemFile('D:/test/f.xml')
我想替换该行即XML路径字符串与不同的XML路径的一部分:
例子:
a.setSystemFile('C:/try/X.xml')
我的代码如下所示:
with open('script.py') as f: lines = f.read().splitlines()
with open('script.py', 'w') as f:
for line in lines:
if line.startswith('a.setSystemFile'):
f.write(line.replace('D:/test/f.xml','C:/try/X.xml')
但是,这会将该文件渲染为空,并且只写入C:/try/X.xml。有没有办法在保留原始内容的同时,像上面的例子中那样替换XML路径字符串。
任何帮助,将不胜感激。谢谢。
感谢上述方法。我也会考虑使用SED,谢谢你的信息。 – user741592 2011-06-01 10:07:29