Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 25th, 2008, 02:20 PM
ironroot ironroot is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2008
Posts: 2 ironroot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old January 26th, 2008, 06:07 AM
Arby Arby is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2006
Location: North Yorkshire, England
Posts: 267 Arby User rank is Second Lieutenant (5000 - 10000 Reputation Level)Arby User rank is Second Lieutenant (5000 - 10000 Reputation Level)Arby User rank is Second Lieutenant (5000 - 10000 Reputation Level)Arby User rank is Second Lieutenant (5000 - 10000 Reputation Level)Arby User rank is Second Lieutenant (5000 - 10000 Reputation Level)Arby User rank is Second Lieutenant (5000 - 10000 Reputation Level)Arby User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
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:
Code:
>>> os.system(g++)
  File "<stdin>", line 1
    os.system(g++)
                 ^
SyntaxError: invalid syntax
whereas if I add double quotes
Code:
>>> os.system("g++")
g++: no input files
256
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.

Hope that helps
Arby

Reply With Quote
  #3  
Old February 18th, 2008, 10:37 AM
crescere crescere is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2007
Location: Wisconsin
Posts: 13 crescere User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 56 m 2 sec
Reputation Power: 0
Just use this:

import os
os.system('system command you want to run')

Reply With Quote
  #4  
Old February 18th, 2008, 02:29 PM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is offline
Commie Mutant Traitor
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2004
Location: Lost in the suburban jungles of Atlanta
Posts: 1,561 Schol-R-LEA User rank is General 6th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 6th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 6th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 6th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 6th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 6th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 6th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 6th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 6th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 6th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 6th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 6th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 6th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 6th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 6th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 6th Grade (Above 100000 Reputation Level) 
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:
Code:
    os.system('g++ -lm -O3 ' +  outfile.name + \ 
              ' OMEC.cpp RandomOMEC.cpp -o evolve');

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.

You could also use formatted string interpolation if it is a complicated string:
Code:
    os.system('g++ -lm -O3 %(file)s OMEC.cpp RandomOMEC.cpp -o evolve' \
              % {'file': outfile.name});
but that would probably be overkill for a single insertion.
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
#define KINSEY (rand() % 7) λ Scheme is the Red Pill
Scheme in ShortUnderstanding the C/C++ Preprocessor
Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > How to run a command line from withing a python program


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
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.

 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

 
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.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.


© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 7 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek