python - 在字符串中插入变量
问题描述
我正在尝试使用 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()
解决方案
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()
推荐阅读
- python-3.x - 如何检查这个双端队列在 Python 中是否为空?
- azure - 我可以自动扩大/缩小 Azure 应用服务计划吗?
- javascript - 如何使用移动设备的触摸事件在固定容器中滚动图像?
- python-3.x - pandas DataFrame 列的条件格式(单元格突出显示)
- javascript - 如何在 mustache 模板中使用 JavaScript_button?
- html - 正则表达式查找 html 类名
- angularjs - AngularJs 视图加载超过 1300 项需要太长时间
- keyboard - 死键在 Remmina,RDP 连接中不起作用
- kubernetes - 是否可以在 kubeadm 集群中创建集群 Autoscaler?
- reactjs - 为什么 useState 不呈现 API 响应?