Python-无法从文件中读取值

Python-Fab not reading value from file

sed 命令没有从文件中读取值。

with open('file.txt') as f:
 content = f.read()
subprocess.call("sed -i '/name/s/$/%s /' copy_vmlist" % content ,shell=True)

上面的不是working.sed命令应该在copy_vmlist中搜索'name'并将file.txt的内容(jega)附加到下一个colmun。

Content of file.txt:
jega

Content of copy_vmlist:
Age
name
degree

Expected output in copy_vmlist:
Age
name jega
degree

问题出在您的 sed 命令参数中。我猜您想将 "copy_vmlist" 文件中的 "name" 字符串替换为 "jega"。试一试:

#!/usr/bin/python
import subprocess

with open('file.txt') as f:
  content = f.read().strip()
  cmd = "sed -i 's/name/%s/' copy_vmlist" % content
  subprocess.call(cmd ,shell=True)

编辑:

编辑完成后,您需要:

cmd = "sed -i 's/name/name %s/' copy_vmlist" % content