Note that sprocess.stdin.write('/stop'.encode()) will not add a newline! Your subprocess is probably expecting a line of input, signified by either a newline or EOF. Closing stdin will send that EOF.
Try sending sprocess.stdin.write('/stop\n'.encode()) instead.
python - From commands to subprocess - Stack Overflow
Using subprocess to send commands to command prompt from python
python:send command with subprocess - Stack Overflow
python - How to send a new command to a subprocess - Stack Overflow
Videos
The equivalent command to commands.getoutput is subprocess.check_output:
from subprocess import check_output
out = check_output('tr -d "\'" < /tmp/file_1.txt > /tmp/file_2.txt', shell=True)
import subprocess
p=subprocess.Popen('tr -d "\'" < /tmp/file_1.txt > /tmp/file_2.txt',shell=True,stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output=p.communicate()[0]
print "o", output
I'm making a script to activate venv for me, for convenience and to help me learn a little more.
I'm trying to make it work for all/most Windows versions, so that it's something I can use regardless of which computer / project I'm using.
I'm trying to get this work using subprocess, but I can't figure out how to send a second command.
This is what I've got (bit of a mess, been testing with it a lot):
process = subprocess.Popen('start cmd /k', shell=True, stdout=subprocess.PIPE)
As can be seen, this starts CMD. Does this fine, pulls up the window and that's great! However, I want to get the working directory, and cd to 'CWD'\\venv\\Scripts.
So I made a separate file, cwd_venvwhich is literally 2 lines:
def get_current_location():
venv_dir = print(os.getcwd())
The idea is that this file will be in the project directory, and activate_venv will be a python package (or something, not sure how to correctly set that up yet, not looked it up at all).
The main file will ask which project is needed (all my projects are in a local repo, in program files to ensure I can access it from all/most my computers).
Anyway, the main code:
dirx = cwd_venv.get_current_location() venvdir = os.path.abspath(os.path.join(os.getcwd(), 'venv\\Scripts'))
so as mentioned before, I have a line that opens CMD.
process = subprocess.Popen('start cmd /k', shell=True, stdout=subprocess.PIPE)But I want to be able to change the directory to venvdir. How might I go about this?
process.communicate('cd' + venvdir)
I know that the ('cd' +venvdir) probably isn't right, I've changed it a thousand times trying to figure it.
I find it hard to search this up online, everything that comes up is the opposite. "How to run a python script in CMD" and stuff like that, which is why I'm struggling. I've looked at the subprocess docs, and from what I understand the subprocess.communicate should send a command? (Might be wrong, again)
your code will send the text
import nukeprint nukequit()
with no newline, thus the python instance will not try to execute anything, everything is just sitting in a buffer waiting for a newline
The subprocess module is not intended for interactive communication with a process. At best, you can give it a single pre-computed standard input string and then read its stdout and stderr:
p = Popen(..., stdin=PIPE, stdout=PIPE, stderr=PIPE)
out, err = p.communicate(predefined_stdin)
If you actually need interaction, consider using pexpect.