How to run a command line from withing a python program
Discuss How to run a command line from withing a python program in the Python Programming forum on Dev Shed. How to run a command line from withing a python program Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
The ASP Free website provides in-depth information on the latest developer tools available from Microsoft. Our cadre of writers, highly experienced industry experts, reveals the best ways to use established technologies as well as new and emerging technologies. Our coverage of Microsoft's development and administration technologies is among the most respected in the IT industry today.
ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month! Download and Activate to enter!
Intel® Graphics Performance Analyzers is a powerful tool suite for analyzing and optimizing your games, media, and graphics-intensive applications. Used by some of the best developers on the planet, Intel GPA lets you maximize your app’s performance.
Posts: 2
Time spent in forums: 28 m 43 sec
Reputation Power: 0
How to run a command line from withing a python program
Hi all
I have been working on this program for a few days and got most of the stuff to work so far but I am stuck on the last part which is I need to call a command line from with in my python program
Code:
import re
import os
test = "#define MEV"
times = range(1)
for i in times:
runNum = str(i)
run = "#define MEV " + runNum
outfile=open('Temp.cpp', 'w')
infile = open('Experiment2.1.cpp', 'r')
for line in infile:
if re.search(test, line):
outfile.write(run)
outfile.write("\n")
print"here"
else:
outfile.write(line)
os.system(g++ -lm -O3 outfile OMEC.cpp RandomOMEC.cpp -o evolve)
os.system(evolve)
The only problem I am having is getting the last two lines to work. I need to call g++ compiler and the execute the evolve file that it makes any help would be great
Posts: 267
Time spent in forums: 4 Days 19 h 29 m 59 sec
Reputation Power: 62
Hi,
what error message do you get when you try to run the code? At first glance it looks like you are missing some quotes from the os.system call. For example:
Presumably if I added your full command string in the quotes it should work but I know nothing about C. Therefore I don't know the proper way to execute a C program but changing os.system(evolve) to os.system("evolve") might be worth a try.
Also it's probably worth looking to see if there are some proper bindings for using C in python.
Posts: 1,713
Time spent in forums: 1 Month 2 Weeks 1 Day 22 m 14 sec
Reputation Power: 1495
Just to clarify the issue: the argument for os.system() is a string, which is used as a shell command. The way you had written it, it was trying to pass it the sum of non-existent variables named 'g', 'lm', etc.
If you need to pass the value of a variable to the system command, you need to convert the value to a string and concatenate it to the command string:
Note that I passed it the name of the file, not the file object itself; the shell knows nothing about files or Python objects, and even if it did, trying to concatenate a string with a file handle makes about as much sense trying to add the number three to the color blue.
FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
Last edited by Schol-R-LEA : February 18th, 2008 at 03:32 PM.