By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,846 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,846 IT Pros & Developers. It's quick & easy.

Running external programs with specifications from python script

P: 2
Hey,

I'm fairly new to using python, but I started in my computer science course and I really enjoy using it. I was wondering if I could use python to run a program in bash where the python script specifies options such as:

Expand|Select|Wrap|Line Numbers
  1. mencoder -dvd 1 -oac copy -ovc copy -o movie-rip.avi
Is there any way I can change the options through an interactive python program? Or is it just ridiculously complicated?

Thanks for the consideration.
Nov 24 '07 #1
Share this Question
Share on Google+
4 Replies


bartonc
Expert 5K+
P: 6,596
Hey,

I'm fairly new to using python, but I started in my computer science course and I really enjoy using it. I was wondering if I could use python to run a program in bash where the python script specifies options such as:

Expand|Select|Wrap|Line Numbers
  1. mencoder -dvd 1 -oac copy -ovc copy -o movie-rip.avi
Is there any way I can change the options through an interactive python program? Or is it just ridiculously complicated?

Thanks for the consideration.
Not complicated at all:
Expand|Select|Wrap|Line Numbers
  1. # unix command line :mencoder -dvd 1 -oac copy -ovc copy -o movie-rip.avi
  2. # 1st make some lists
  3.  
  4. Cmd = 'mencoder'
  5. CmdOptions = ('-dvd', '-oac', '-ovc', '-o')
  6. ReadableOptions = ('use', 'meaingful', 'terms', 'output file name')
  7.  
  8. # a dictionary for the results of user interaction
  9. optionsDict = {} # empty, for now
  10.  
  11. # the interactive function
  12. def GetOptionFromUser(readableName):
  13.     return raw_input('Enter the value for %s: ' %(readableName))
  14.  
  15. # the "script"
  16. for i, option in enumerate(ReadableOptions): # this is how we count iteration
  17.     optionsDict[CmdOptions[i]] = GetOptionFromUser(option)
  18.  
  19.  
  20. # create the command string
  21. cmdStr = "%s %s" %(Cmd, " ".join([("%s %s" %(key, optionsDict[key])) for key in CmdOptions]))
  22.  
  23. # then, call os.system(cmdStr), os.spawn(), etc. depending on your needs
  24. # Here, just print the command line:
  25.  
  26. print cmdStr
  27.  
output after typing in only the values:
mencoder -dvd 1 -oac copy -ovc copy -o movie-rip.avi
Nov 25 '07 #2

bartonc
Expert 5K+
P: 6,596
Not complicated at all:
Expand|Select|Wrap|Line Numbers
  1. # unix command line :mencoder -dvd 1 -oac copy -ovc copy -o movie-rip.avi
  2. # 1st make some lists
  3.  
  4. Cmd = 'mencoder'
  5. CmdOptions = ('-dvd', '-oac', '-ovc', '-o')
  6. ReadableOptions = ('use', 'meaingful', 'terms', 'output file name')
  7.  
  8. # a dictionary for the results of user interaction
  9. optionsDict = {} # empty, for now
  10.  
  11. # the interactive function
  12. def GetOptionFromUser(readableName):
  13.     return raw_input('Enter the value for %s: ' %(readableName))
  14.  
  15. # the "script"
  16. for i, option in enumerate(ReadableOptions): # this is how we count iteration
  17.     optionsDict[CmdOptions[i]] = GetOptionFromUser(option)
  18.  
  19.  
  20. # create the command string
  21. cmdStr = "%s %s" %(Cmd, " ".join([("%s %s" %(key, optionsDict[key])) for key in CmdOptions]))
  22.  
  23. # then, call os.system(cmdStr), os.spawn(), etc. depending on your needs
  24. # Here, just print the command line:
  25.  
  26. print cmdStr
  27.  
output after typing in only the values:
mencoder -dvd 1 -oac copy -ovc copy -o movie-rip.avi
Line 21 has a lot going on in it. Here is that line broken down into its constituent parts:
Expand|Select|Wrap|Line Numbers
  1.  
  2. optionList = [] # an empty list
  3. for key in CmdOptions:
  4.     optionList.append("%s %s" %(key, optionsDict[key])
  5. optionStr = " ".join(optionList)
  6. cmdStr = "%s %s" %(Cmd, optionStr)
Nov 25 '07 #3

P: 2
Thanks a lot guys. You really helped me out. I'll just chew through the code, figure out what it is that you did, and get it to work for me. lol
Nov 25 '07 #4

bartonc
Expert 5K+
P: 6,596
Thanks a lot guys. You really helped me out. I'll just chew through the code, figure out what it is that you did, and get it to work for me. lol
I just caught an error on line 3. Here's the correction:
Expand|Select|Wrap|Line Numbers
  1. #
  2.     optionList.append("%s %s" %(key, optionsDict[key])) # missing paren
Nov 25 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.