Controlling a python script from another script

后端 未结 4 1630
失恋的感觉
失恋的感觉 2020-12-11 12:55

I am trying to learn how to write a script control.py, that runs another script test.py in a loop for a certain number of times, in each run, reads

4条回答
  •  情歌与酒
    2020-12-11 13:37

    You can use subprocess module or also the os.popen

    os.popen(command[, mode[, bufsize]])
    

    Open a pipe to or from command. The return value is an open file object connected to the pipe, which can be read or written depending on whether mode is 'r' (default) or 'w'.

    With subprocess I would suggest

    subprocess.call(['python.exe', command])
    

    or the subprocess.Popen --> that is similar to os.popen (for instance)

    With popen you can read the connected object/file and check whether "Stop now" is there.

    The os.system is not deprecated and you can use as well (but you won't get a object from that), you can just check if return at the end of execution.

    From subprocess.call you can run it in a new terminal or if you want to call multiple times ONLY the test.py --> than you can put your script in a def main() and run the main as much as you want till the "Stop now" is generated.

    Hope this solve your query :-) otherwise comment again.

    Looking at what you wrote above you can also redirect the output to a file directly from the OS call --> os.system(test.py *args >> /tmp/mickey.txt) then you can check at each round the file.

    As said the popen is an object file that you can access.

提交回复
热议问题