Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
subprocess with args
#1
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question How to compare two parameters in a function that has *args? Milan 4 1,198 Mar-26-2023, 07:43 PM
Last Post: Milan
  *args implementation and clarification about tuple status amjass12 10 3,930 Jul-07-2021, 10:29 AM
Last Post: amjass12
  [SOLVED] Good way to handle input args? Winfried 2 2,004 May-18-2021, 07:33 PM
Last Post: Winfried
  Two Questions, *args and //= beginner721 8 3,422 Feb-01-2021, 09:11 AM
Last Post: buran
  does yield support variable args? Skaperen 0 1,643 Mar-03-2020, 02:44 AM
Last Post: Skaperen
  is there a way: repeat key word args Skaperen 2 2,200 Feb-03-2020, 06:03 PM
Last Post: Skaperen
  Using function *args to multiply multiple arguments allusernametaken 8 5,951 Nov-20-2019, 12:01 AM
Last Post: allusernametaken
  Passing string args to Popen CardBoy 3 4,198 Jan-16-2018, 09:22 AM
Last Post: Gribouillis
  Why args type is always tuple, when passed it as argument to the function. praveena 5 5,257 Jan-16-2018, 09:07 AM
Last Post: praveena
  how to use *args haye 7 5,180 Sep-25-2017, 01:21 PM
Last Post: haye

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020