472,965 Members | 2,135 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,965 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 1479
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

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

Similar topics

2
by: Jorgen Grahn | last post by:
I couldn't think of a good solution, and it's hard to Google for... I write python command-line programs under Win2k, and I use the bash shell from Cygwin. I cannot use Cygwin's python package...
4
by: Peter Otten | last post by:
Is there a way to limit both width and height of a canvas text item? My current workaround seems clumsy: import Tkinter as tk root = tk.Tk() canvas = tk.Canvas(root, width=400, height=200,...
5
by: Shalen chhabra | last post by:
Hey, Can anyone give me a snippet for running a python program over all the files in the directory. For ex: I have ten files in a directory and I want to run a python program against all of...
2
by: Mike | last post by:
We have a large c++ program that runs under Windows NT Embedded to control an instrument. That program has been written to run external python scripts. The computer where this code runs does not...
3
by: Edg Bamyasi | last post by:
This Is A Late Cross Post from comp.lang.python. It seems the mistery is deeper then i expected. What is the running time of conactination on character strings. i.e. >> joe="123" >>...
13
by: John Salerno | last post by:
If I want to write my code in a separate text editor (I like UltraEdit) but then press a single button to have that code run in the IDLE environment, is that possible? I know that you can configure...
2
by: Heikki Toivonen | last post by:
We have successfully used a script to run external programs for several years. Now we upgraded our Python to 2.5, and are hitting a mysterious error. The expected output from the sample script...
51
by: Ojas | last post by:
Hi!, I just out of curiosity want to know how top detect the client side application under which the script is getting run. I mean to ask the how to know whether the script is running under...
9
by: Jimmy | last post by:
Well, i know it may be a little non-python thing, however, I can think of no place better to post this question :) can anyone tell me, in python, how to obtain some information of a running...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.