4

There is an excel file which contains the paths of multiple scenarios. I am using os.system(command) in a for loop. In each iteration, a path is read from excel file and executes the scenario for that path.

My problem is that every time, by using os.system(), a CMD opens, execute one command and close. In next iteration, again second path is read and execute it and CMD close. Here CMD pop-ups again and again. And the system is busy during that period and not able to do other task. I want to execute all the commands(scenario) in one CMD because I would like to minimize it and use the system for other task.

In each iteration, there are two main steps:

  1. os.chdir(PATH)
  2. os.system(path of exe+" "+name of config file that is present at PATH")

Can it be done by using subprocess. If yes please give me some example how it can be implemented?

2
  • 1
    Could you post the code you've used to run the contents of the Excel file? Commented Dec 6, 2011 at 10:45
  • 1
    What do you mean by "the system"? The Python interpreter? That should block during os.system. Commented Dec 6, 2011 at 10:46

3 Answers 3

2

If you want to use the subprocess module, try something like this :

from subprocess import call
import os.path

def call_scenario(path, config_file):
    retcode = call(["path/of/exe", os.path.join(path,config_file)])
    if retcode != 0:
       print "Something bad happened : %s"%retcode 

When using subprocess.call, the shell=False parameter will avoid to launch a cmd to do something.

Sign up to request clarification or add additional context in comments.

Comments

0

it can be done - here is a quick example using multiprocessing (Python 2.6 or newer )

The example bellow uses a Unix command ("ls") and unixes paths ("/usr, etc...) but just replace those with your needed commands and paths.

from multiprocessing import Process
import os

paths = ["/tmp", "/usr", "/usr/include"]


def exec_(path):
    p = Process()
    p.run = lambda: os.system("ls %s" % path)
    p.start()

for path in paths:
    exec_(path)

Another option, if you need some sophisticated control on what is running, return codes, etc... is to make use of the Fabric project - Although it is aimed at executingmultiple commands on different hosts using ssh - I think it culd be used for different paths on the same host.

URL for fabric:
http://docs.fabfile.org/en/1.3.3/index.html

3 Comments

you're speaking of subprocess while using multiprocessing in your example, and I'm not sure that ls -l will work on the OP computer as he's talking about exe, Excel and CMD that sounds like a windows...
Unfortunately @CédricJulien is right - at least when it comes to the "Windows" part.
"multiprocessing", sorry (fixed) - as for the posix example, this is an example code - it will work if one replace the parameters and the external command for whatever
0

To run c:\path\to\exe for all config.ini from each path simultaneously and to change current directory to cwd before it is executed:

from subprocess import Popen

processes = [Popen([r"c:\path\to\exe", "config.ini"], cwd=path) for path in paths]
for p in processes: p.wait()

If you don't want to run all commands in parallel then use subprocess.call() with the same arguments as for subprocess.Popen().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.