我可以在命令行上成功运行但在python脚本中遇到问题。它抱怨第二个双引号。
pysed -r "192.168.33.10" "$NEW_IP" FILE --write
^
SyntaxError: invalid syntax
如何在脚本中运行?
答案 0 :(得分:1)
该模块确实没有将其用作库的文档。但是挖掘源代码,你可以弄清楚如何使用它。
例如:
import shlex
from pysed import main as pysedmain
pattern = '192.168.33.10'
new_ip = '192.168.0.1'
filename = '/path/to/my/file.txt'
command_line_args = '-r "{pattern}" "{replacement}" {filename}'.format(pattern=pattern, replacement=new_ip, filename=filename)
args = shlex.split(command_line_args)
isWrite = True
with open(filename, 'rU') as f:
data = f.read()
pysedmain.executeArguments(args, data, filename, isWrite)