SwimBikeRun
SwimBikeRun

Reputation: 4460

Execute script from within python

I have a playgame.cmd file I would like to exceute from within my python code.

It is a genetic algorithm that runs the game (input is individual), waits for the game to run with that individual, then parses data from the game log to output the fitness of that individual.

Inside the .cmd file (shouldn't matter I don't think):

python tools/playgame.py "python MyBot.py" "python tools/sample_bots/python/HunterBot.py"
--map_file tools/maps/example/tutorial1.map --log_dir game_logs --turns 60 --scenario 
--food none --player_seed 7 --verbose -e

(This is for the ants AI challenge if you were wondering)

This is all details though. My question is that of the title: How do I start the script midline in python, wait for the script to finish, then resume the python execution? The script file is in the same folder as the python AntEvolver.py file.

Upvotes: 0

Views: 2450

Answers (2)

tito
tito

Reputation: 13251

A very little snippet:

import subprocess
# do your stuff with sys.argv
subprocess.Popen("python MyBot.py", shell=True).communicate()
# script executed and finished, you can continue...

Upvotes: 0

John Zwinck
John Zwinck

Reputation: 249133

If you want to launch a .cmd file from within a Python script which then launches two more copies of Python within the .cmd, I think you need to slow down, take a step back, and think about how to just get all this stuff to run within one Python interpreter. But, the direct answer to your question is to use os.system() (or the subprocess module, which is also mentioned here):

http://docs.python.org/library/os.html#os.system

Upvotes: 3

Related Questions