Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming
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.


Tutorials
| Forums

Download to Enter
| Contest Rules

DOWNLOAD INTEL® GPA FOR FREE

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, 03: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, 07: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: 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:
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, 11: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, 03: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: Norcross, GA (again)
Posts: 1,713 Schol-R-LEA User rank is General 8th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 8th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 8th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 8th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 8th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 8th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 8th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 8th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 8th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 8th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 8th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 8th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 8th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 8th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 8th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 8th Grade (Above 100000 Reputation Level) 
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:
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 03: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

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


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

© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap