Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Sorry I'm kind of a noob at this:

Basically I've downloaded this package that deals with text files that is run from the terminal, but the command I need takes in two arguments. I also need to do this many times (5000+).

I need to get these arguments with a python script, and can loop it many times using python. So could I get the arguments within the python script, run the command-line line, and loop it? How?


share|improve this question
This might be better off on because it is not specifically related to Ubuntu. – David Edwards Jun 12 '13 at 13:12
@confoosed -- You need to edit to add "I've downloaded this package to my Ubuntu box...". </halfserious> – belacqua Jun 12 '13 at 21:31
Are you saying you need to have python actually execute built-in shell/bash functions? That can be done (e.g., see V's popen example), but you could use some more details in the question. – belacqua Jun 12 '13 at 21:33

Here's a fish...

import popen2, sys

def loopy_subprocess(arga, argb, iterations, command = 'echo'):
    for i in range(int(iterations)):
        p = popen2.Popen4((command, arga, argb))

if __name__ == '__main__':
        arga = sys.argv[1]
        argb = sys.argv[2]
        iterations = sys.argv[3]
        print("You didn't supply enough arguments\n"+\
              "Usage: python arga argb iterations\n"+\
              "Warning - unsafe.  No input validation and doesn't account for spaces in arguments\n"+\
              "Optionally supply the command to be run as a final argument\n")
        command = sys.argv[4]
        loopy_subprocess(arga, argb, iterations, command)
        loopy_subprocess(arga, argb, iterations)
share|improve this answer
+1 for 'here's a fish' .... (actually, I was going to upvote it anyway) – belacqua Jun 12 '13 at 21:31
@belacqua - I could have used call but it doesn't support wait. i thought wait was necessary given that the want to run batches of 5000 (I also think threading is necessary, but the question doesn't merit a threading answer) blah blah blah. – RobotHumans Jun 12 '13 at 21:37
Change the popen2 calls to the subprocess module. popen2 and family are deprecated in Python 2 and removed from Python 3. Don't use quit() or exit(), they are added by at runtime and do not work in all circumstances. Use sys.exit() or raise a SystemExit. Bonuspoint for using something like argparse. – Timo Jun 13 '13 at 9:33
@Timo - feel free to edit the changes in... it won't bother me at all, and if it makes a better answer great. i guess old habits die hard. – RobotHumans Jun 13 '13 at 13:52

Probably the easiest way to do this is to access the command line arguments from within python and also do the loop within python. You could look into sys.argv from the sys module to access the command line arguments. If I recall correctly that should allow you to do something like:

python argument1 argument2

and then access the arguments within using

import sys
arg_1 = sys.argv[1]
arg_2 = sys.argv[2]

You could also develop a whole stack of commands easily with a tool like cliff (Command Line Interface Formulation Framework) .

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.