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.
Take your business to the next level, with "The
Edge of Success: 9 Building Blocks to Double Your Sales."
Inside, you will discover how to easily capture more leads,
get repeat sales, and significantly grow your business.
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: 61
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,561
Time spent in forums: 1 Month 1 Week 4 Days 6 h 21 m 48 sec
Reputation Power: 1365
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 02:32 PM.
Create the Optimal Architecture for your Critical Applications Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.
Gartner Magic Quadrant for Application Delivery Controllers Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.
Knowledge is Power What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.
Rationalizing the Multi-Tool Environment The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.