sed 命令未从文件中读取值。
with open('file.txt') as f:
content = f.read()
subprocess.call("sed -i '/name/s/$/%s /' copy_vmlist" % content ,shell=True)
上面的命令不起作用。sed 命令应该在 copy_vmlist 中搜索“名称”并将 file.txt 的内容(jega)附加到下一个列。
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
关于Python-Fab 不从文件读取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53461385/