473,396 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1489
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.