gpt4 book ai didi

python - 将 bash/shell 行转换为 python 2.6

转载 作者:行者123 更新时间:2023-12-01 04:37:52 25 4
gpt4 key购买 nike

我对编程比较陌生,尤其是 BASH 和 python 以及这个网站。抱歉发了多个帖子!我正在尝试将这一行放入 python 中。我尝试过 os.popen。你们还有什么其他方法可以想到如何做到这一点。我仅限于 python v2.6,无法升级到较新的版本,否则我会知道如何在 3.whatever 中执行此操作。谢谢!

 sample1=($(/bin/cat /proc/meminfo | egrep 'MemTotal|MemFree|Cached|SwapTotal|SwapFree|AnonPages|Dirty|Writeback|PageTables|HugePages_' | awk ' { print $2} ' | pr -t -T --columns=15 --width=240))

这是我在 python 中的内容,但它不起作用。任何人都知道如何重新排列它,使其与 BASH 中的行相同。我知道这些不应该是 elif。老实说,我很困惑,不知道从这里该去哪里。

lst = []  #
inFile = open('/proc/meminfo') # open file
line = inFile.readline()
sample1 = {} #
while(line): #
if line.find('MemTotal'):
line = line.split()
sample1['MemTotal'] = line[1]
elif line.find('MemFree'):
line = line.split()
sample1['MemFree'] = line[1]
elif line.find(line, 'Cached'):
line = line.split()
sample1['Cached'] = line[1]
elif line.find(line, 'SwapTotal'):
line = line.split()
sample1['SwapTotal'] = line[1]
elif line.find(line, 'SwapFree'):
line = line.split()
sample1['SwapFree'] = line[1]
elif line.find(line, 'AnonPages'):
line = line.split()
sample1['AnonPages'] = line[1]
elif line.find(line, 'Dirty'):
line = line.split()
sample1['Dirty'] = line[1]
elif line.find(line, 'Writeback'):
line = line.split()
sample1['WriteBack'] = line[1]
elif line.find(line, 'PageTables'):
line = line.split()
sample1['PageTables'] = line[1]
elif line.find(line, 'HugePages_'):
line = line.split()
sample1['HugePages'] = line[1]

最佳答案

这应该通过 subprocess.Popen 管道输出来从 python 运行 bash 命令。并适用于 python2.6:

from subprocess import Popen, PIPE
p1 = Popen(["cat","/proc/meminfo"], stdout=PIPE)
p2 = Popen(["egrep", 'MemTotal|MemFree|Cached|SwapTotal|SwapFree|AnonPages|Dirty|Writeback|PageTables|HugePages_' ], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close()
p3 = Popen(["awk","{ print $2}"],stdin=p2.stdout,stdout=PIPE)
p2.stdout.close()
p4 = Popen(["pr", "-t", "-T", "--columns=15", "--width=240"],stdin=p3.stdout,stdout=PIPE)
p3.stdout.close()

output = p4.communicate()
print(output[0])

我的系统的输出是:

16341932    4484840     5105220     0       8388604     8388604     108     0       5106832     78100       0       0       0       0       0

您还可以使用python打开文件并将文件对象传递给第一个进程:

from subprocess import Popen,PIPE,STDOUT
with open("/proc/meminfo") as f:
p1 = Popen(["egrep", 'MemTotal|MemFree|Cached|SwapTotal|SwapFree|AnonPages|Dirty|Writeback|PageTables|HugePages_' ], stdin=f, stdout=PIPE)
p2 = Popen(["awk","{ print $2}"],stdin=p1.stdout,stdout=PIPE)
p1.stdout.close()
p3 = Popen(["pr", "-t", "-T", "--columns=15", "--width=240"],stdin=p2.stdout,stdout=PIPE)
p2.stdout.close()

output = p3.communicate()
print(output[0])

一个纯Python解决方案,使用str.find模仿egrep查找包含文件中pre的任何子字符串的行,并使用str.rsplit获取第二个列,即数字:

pre = ('MemTotal', 'MemFree', 'Cached', 'SwapTotal', 'SwapFree', 'AnonPages', 'Dirty', 'Writeback', 'PageTables', 'HugePages_')
with open("/proc/meminfo") as f:
out = []
for line in f:
# if line.find(p) is not -1 we have a match
if any(line.find(p) != -1 for p in pre):
# split twice from the end on whitespace and get the second column
v = line.rsplit(None, 2)[1]
out.append(v)
print(" ".join(out))

输出:

16341932   4507652   5128624   0   8388604   8388604   48   0   5059044   78068   0   0   0   0   0

使用any上面的代码将延迟评估并短路匹配,如果没有匹配,它将评估为 False,因此不会添加任何内容。

更忠实于egrep,我们可以使用re.search编译要检查的模式/子字符串:

import  re
r = re.compile(r"MemTotal|MemFree|Cached|SwapTotal|SwapFree|AnonPages|Dirty|Writeback|PageTables|HugePages_")
with open("/proc/meminfo") as f:
out =[]
for line in f:
if r.search(line):
v = line.rsplit(None, 2)[1]
out.append(v)
print(" ".join(out))

输出:

16341932   4507596   5128952   0   8388604   8388604   0   16788   5058092   78464   0   0   0   0   0

Python 就是 Python,我们可以将所有逻辑放在一个列表 comp 中来获取数据:

pre = ('MemTotal', 'MemFree', 'Cached', 'SwapTotal', 'SwapFree', 'AnonPages', 'Dirty', 'Writeback', 'PageTables', 'HugePages_')
with open("/proc/meminfo") as f:
out = [line.rsplit(None, 2)[1] for line in f if r.search(line)]
print(" ".join(out))

输出:

16341932   4443796   5133420   0   8388604   8388604   120   0   5118004   78572   0   0   0   0   0

关于python - 将 bash/shell 行转换为 python 2.6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31415592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com