1

I need to translate this line of Bash into Python:

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))

I know I need to use Popen and communicate I just don't know how to rearrange it.

Any help?

1
  • You may get the support you need here, but your probably best off asking this question in SO or Unix & Linux.
    – nixpower
    Jul 3, 2015 at 15:19

1 Answer 1

4

You don't need popen - popen is what you use to start a process. We don't need to start another process.

And you don't need to be piping things to/from STDIN/STDOUT ;

/proc/meminfo follows the same rules as everything else in *nix.

  • Everything is a file

You can just open /proc/meminfo and process it like you would any other file.

f = open('/proc/meminfo')
for l in f:
    # do stuff with the line from the file
  • Create a dictionary of the line items you want
  • Find those items (write a function is_interesting(line) or something)
  • Do the string processing you need

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .