#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2008
    Posts
    2
    Rep 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
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2006
    Location
    North Yorkshire, England
    Posts
    267
    Rep Power
    65
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2007
    Location
    Wisconsin
    Posts
    13
    Rep Power
    0
    Just use this:

    import os
    os.system('system command you want to run')
  6. #4
  7. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,806
    Rep Power
    1570
    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.
    Last edited by Schol-R-LEA; February 18th, 2008 at 03:32 PM.
    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 Short Understanding the C/C++ Preprocessor
    Taming Python A 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

IMN logo majestic logo threadwatch logo seochat tools logo