471,350 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

Running external programs with specifications from python script

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
4 1322
bartonc
6,596 Expert 4TB
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
6,596 Expert 4TB
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
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
6,596 Expert 4TB
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.

Similar topics

2 posts views Thread by Jorgen Grahn | last post: by
4 posts views Thread by Peter Otten | last post: by
3 posts views Thread by Edg Bamyasi | last post: by
13 posts views Thread by John Salerno | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.