首页 > 解决方案 > 在字符串中插入变量

问题描述

我正在尝试使用 popen 进行 for 循环,该循环使用变量 .How to 在 for 循环中使用“line”变量,该循环使用 os.open for linux 命令。

os.popen("yum history | awk '{print $1}' | grep -v 'Loaded\|history\|ID\|-' > ids.txt" ).read()
myfile = open("ids.txt", "r")
for line in myfile:
     first=os.popen("yum history info + line + | grep -i Command | awk '{print $4,$5}'").read().strip()
#     first="yum history info +line | grep -i Command "
     print(line)
     print(first)
myfile.close()

标签: python

解决方案


os.popen("yum history | awk '{print $1}' | grep -v 'Loaded\|history\|ID\|-' > ids.txt" ).read()
myfile = open("ids.txt", "r")
for line in myfile:
     first=os.popen("yum history info " + line + "| grep -i Command | awk '{print $4,$5}'").read().strip()
#     first="yum history info +line | grep -i Command "
     print(line)
     print(first)
myfile.close()

f字符串:

os.popen("yum history | awk '{print $1}' | grep -v 'Loaded\|history\|ID\|-' > ids.txt" ).read()
myfile = open("ids.txt", "r")
for line in myfile:
     first=os.popen(f"yum history info {line} | grep -i Command | awk '{{print $4,$5}}'").read().strip()
#     first="yum history info +line | grep -i Command "
     print(line)
     print(first)
myfile.close()

推荐阅读