简体   繁体   中英

Python-how to send commands to existing shell?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM