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.

Answer from MisterMiyagi on Stack Overflow
🌐
Python
docs.python.org › 3 › library › subprocess.html
subprocess — Subprocess management
1 day ago - Use Popen.communicate() when using pipes to avoid that. ... When the timeout parameter is not None, then (on POSIX) the function is implemented using a busy loop (non-blocking call and short sleeps). Use the asyncio module for an asynchronous wait: see asyncio.create_subprocess_exec. Changed in version 3.3: timeout was added. ... Interact with process: Send data to stdin.
Discussions

python - From commands to subprocess - Stack Overflow
How to write the following line using subprocess library instead of commands. The idea is to get the same result but using subprocess. commands.getoutput('tr -d "\'" /tmp/ More on stackoverflow.com
🌐 stackoverflow.com
Using subprocess to send commands to command prompt from python
You can use multiline strings to pass multiple commands at once. I'd also drop the start and just execute cmd as a child so it returns when your commands are complete. Something like this: command = """cd /somedir/ activate.bat """ subprocess.run(args=['cmd.exe', command]) But keep in mind I think this will only activate the venv within the context of this subprocess process. Your current python interpreter won't be affected. More on reddit.com
🌐 r/learnpython
6
2
October 14, 2019
python:send command with subprocess - Stack Overflow
I am using ipmitool to start an serial over lan connection on the VSP of an HP. I am trying to send the break command with the letter c. p = subprocess.Popen(CMD + " sol activate", shell=True, std... More on stackoverflow.com
🌐 stackoverflow.com
python - How to send a new command to a subprocess - Stack Overflow
I previously asked a question about how to set up a tkinter gui to recieve lines from a subprocess without the entire program hanging. That is now functional. Now, I can't figure out how to send new More on stackoverflow.com
🌐 stackoverflow.com
May 3, 2017
🌐
DataCamp
datacamp.com › tutorial › python-subprocess
An Introduction to Python Subprocess: Basics and Examples | DataCamp
September 12, 2025 - The stdout of the ls command is connected to the stdin of the grep command using subprocess.PIPE, which creates a pipe between the two processes. The communicate() method is used to send the output of the ls command to the grep command and retrieve the filtered output. The Python subprocess module provides a powerful and flexible way to create and interact with child processes, allowing you to run other programs or issue commands from within your Python script.
🌐
Python
docs.python.org › 3 › library › asyncio-subprocess.html
Subprocesses — Python 3.14.3 documentation
February 23, 2026 - Source code: Lib/asyncio/subprocess.py, Lib/asyncio/base_subprocess.py This section describes high-level async/await asyncio APIs to create and manage subprocesses. Here’s an example of how asyncio...
🌐
Reddit
reddit.com › r/learnpython › using subprocess to send commands to command prompt from python
r/learnpython on Reddit: Using subprocess to send commands to command prompt from python
October 14, 2019 -

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)

Find elsewhere
🌐
Real Python
realpython.com › python-subprocess
The subprocess Module: Wrapping Programs With Python – Real Python
January 18, 2025 - You’ve used the subprocess module to execute programs and send basic commands to the shell. But something important is still missing. For many tasks that you might want to use subprocess for, you might want to dynamically send inputs or use the outputs in your Python code later.
🌐
PythonForBeginners
pythonforbeginners.com › home › subprocess and shell commands in python
Subprocess and Shell Commands in Python - PythonForBeginners.com
August 25, 2020 - Popen.communicate() interacts with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate. The optional input argument should be a string to be sent to the child process, or None, if no data should be sent to the child. Basically, when you use communicate() it means that you want to execute the command · In the “More Reading” section below, you can find links to read more about the subprocess module, but also examples.
🌐
GitHub
gist.github.com › waylan › 2353749
Writing to a python subprocess pipe · GitHub
The input argument is passed to Popen.communicate() and thus to the subprocess’s stdin. If used it must be a byte sequence, or a string if encoding or errors is specified or text is true.
🌐
Dataquest
dataquest.io › blog › python-subprocess
Python Subprocess: The Simple Beginner's Tutorial (2023)
February 19, 2025 - In fact, this is the same as passing /usr/local/bin/python -c print('This is a subprocess') to the command line. Most of the code in this article will be in this format because it's easier to show the features of the run function.
🌐
Stack Overflow
stackoverflow.com › questions › 66835513 › send-command-to-subprocess-popen
python - Send command to subprocess.popen() - Stack Overflow
March 27, 2021 - mcserver = subprocess.Popen('C:/Users/FlexGames/Desktop/Minecraft_Server/FTBTrident-1.4.0-1.7.10Server/ServerStart.bat', stdin=subprocess.PIPE) Current part to send a command to console: mcserver.stdin.write(b'stop\n') python · subprocess · minecraft · Share ·
🌐
Simplilearn
simplilearn.com › home › resources › software development › python subprocess: master external command execution
Python Subprocess: Master External Command Execution
December 15, 2025 - Learn about Python's subprocess module for executing external commands. Discover how to manage processes and handle inputs/outputs efficiently.
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
Python Land
python.land › home › interaction with the operating system › python subprocess: run external commands
Python Subprocess: Run External Commands • Python Land Tutorial
October 30, 2024 - Let’s start with a simple call to ls, to list the current directories and files: >>> import subprocess >>> subprocess.run(['ls', '-al']) (a list of your directories will be printed)Code language: Python (python)
🌐
Python.org
discuss.python.org › python help
Subprocess use and inputs - Python Help - Discussions on Python.org
January 24, 2024 - Dear all, I started python code in 2024 (a good resolution is not it ?) to use for a job project and i am currently blocked with the subprocess use. Basically, i have to run python script (written in CPython with CPyth…