Can we execute multiple commands on the same command line launched by python

There is an excel file that contains paths for several scripts. I am using os.system(command) in a for loop. At each iteration, the path is read from the excel file and runs a script for that path.

My problem is that every time using os.system() , CMD opens, execute one command and close it. In the next iteration, the second path is read again and executed, and the CMD is closed. Here the CMD pops up again and again. And the system is busy during this period and cannot perform another task. I want to execute all the commands (scripts) in one CMD, because I would like to minimize it and use the system for other tasks.

At each iteration, there are two main steps:

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

Can this be done using a subprocess. If yes, please give me an example of how this can be implemented?

+4
source share
3 answers

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 running cmd to do something.

+2
source

this can be done - here is a quick example of using multiprocessing (Python 2.6 or later)

The following example uses the Unix ("ls") and unixes ("/ usr, etc.)) commands, but just replace them with the necessary 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 is if you need some kind of sophisticated control over what works, return codes, etc. - use the Fabric project - Although it is designed to run several commands on different hosts using ssh - I think it can be used for different paths on the same host.

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

0
source

To run c:\path\to\exe for all config.ini from each path at the same time and change the current directory to cwd before executing it:

 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 do not want to run all the commands in parallel, use subprocess.call() with the same arguments as for subprocess.Popen() .

0
source

Source: https://habr.com/ru/post/1384882/


All Articles