質問

I have a shell that is opened and I'm running some commands there. my python file should also run 1 command there
How can I do it without opening this shell again ? (I want to use the already opened one) thanks a lot!

editing : It's an .exe that I compiled and i'm opening and running some "init" functions. I want my python script for example to to something like: "func(path to the .exe, commans to execute)" and when using the path - it will use the exsiting .exe

役に立ちましたか?

解決

Use the subprocess module:

import subprocess
subprocess.call("shell commands go in here")

This will execute shell commands from within the location of your Python script. Example:

import subprocess
subprocess.call(["ping", "www.google.com"])
PING www.google.com (178.59.100.242) 56(84) bytes of data.
64 bytes from cache.google.com (178.59.100.242): icmp_req=1 ttl=54 time=19.1 ms
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top