473,585 Members | 2,513 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Running external programs with specifications from python script

2 New Member
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 1493
bartonc
6,596 Recognized Expert Expert
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 Recognized Expert Expert
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
ubuntufox
2 New Member
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 Recognized Expert Expert
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
14348
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 because of a binary module which has to be compiled with Visual C 6. My scripts start with a '#!/usr/bin/env python' shebang, as God intended. ...
4
4145
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, bg="white") canvas.pack() # simulate a clipped text item - never transparent :-( s = "The long and winding road.."
5
5306
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 these files, I wish to do the same using another python code instead of running each of these files one by one, which would be cumbersome giving the...
2
1169
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 and cannot have a full python installation. It ONLY has the pythonNN.dll file that comes with a full python installation. For simple scripts...
3
2305
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" >> joe+="99999999999999999"
13
2808
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 UE to run external tools, but I can't figure out how to run IDLE this way, because when I check on its properties to find it's file path, it is...
2
1879
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 (see below) with 2.4 looks like this: ret else ********************
51
4118
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 Command Prompt or Browser or some other application? Ojas.
9
2169
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 program? paticularly, if i am playing some music in audacious or other media player, how can i get the the name and some other info of current playing...
0
7836
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8199
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7950
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8212
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6606
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5389
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3835
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.