Python-Fab 不从文件读取值

标签 python sed subprocess fabric

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/

相关文章:

python - 在 Windows Python 中将不可搜索的 Pdf 转换为可搜索的 Pdf

bash 方法从 csv 文件中删除最后 4 列

python - 将 pandas 数据帧作为参数传递给 python subprocess.Popen

regex - sed 单行将所有大写字母转换为小写字母?

python - 如何在 Windows 上隐藏子进程的标准输出

python - 当输出很大时如何管理Python中的子进程?

Python有序字典: Why is [] notation required to change dictionary values using a for loop?

Python/Django : What mode for mod_wsgi

python - 尝试使用 chunksize 迭代器除外

bash - 使用 sed 或 awk 删除每行特定索引内容以外的所有内容