Python Forum

Full Version: subprocess with args
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, i've made a python script which gives me some stats from NFS:

import sys
import subprocess
import argparse


parser = argparse.ArgumentParser()
parser.add_argument("-a", action='store_true', help="access")
parser.add_argument("-c", action='store_true', help="close")
parser.add_argument("-r", action='store_true', help="read")
parser.add_argument("-w", action='store_true', help="write")
args = parser.parse_args()

output = subprocess.Popen("nfsstat -l", shell=True)

if len(sys.argv)==1:
        print output
elif args.a:
    subprocess.Popen("nfsstat -l | grep 'access:' | awk '{print $5}' ", shell=True)
elif args.c:
    subprocess.Popen("nfsstat -l | grep 'close:' | awk '{print $5}' ", shell=True)
elif args.r:
    subprocess.Popen("nfsstat -l  | grep 'read:' | awk '{print $5}' ", shell=True)
elif args.w:
    subprocess.Popen("nfsstat -l | grep 'write:' | awk '{print $5}' ",shell=True)
the output of python myscript.py -a is:
Output:
54751
python myscript.py -c:
Output:
3457
is there a better way than i did to make args that would print only one line from the whole output ?
Maybe using PIPE?
thanks