Executing bash with subprocess.Popen

2024/7/7 7:31:09

I'm trying to write a wrapper for a bash session using python. The first thing I did was just try to spawn a bash process, and then try to read its output. like this:

from subprocess import Popen, PIPE
bash = Popen("bash", stdin = PIPE, stdout = PIPE, stderr = PIPE)
prompt = bash.stdout.read()
bash.stdin.write("ls\n")
ls_output = bash.stdout.read()

But this does not work. First, reading from bash's stdout after creating the process fails, and when I try to write to stdin, I get a broken pipe error. What am I doing wrong?

Just to clarify again, I'm not interested in running a single command via bash and then retrieving its output, I want to have a bash session running in some process with which I can communicate via pipes.

Answer

This works:

import subprocess
command = "ls"
p = subprocess.Popen(command, shell=True, bufsize=0, stdout=subprocess.PIPE, universal_newlines=True)
p.wait()
output = p.stdout.read()
p.stdout.close()
https://en.xdnf.cn/q/73350.html

Related Q&A

Attribute error when attempting to get a value for field

Im working with the django rest framework and the serializer Im trying to use is creating errors. Im trying to do something like https://gist.github.com/anonymous/7463dce5b0bfcf9b6767 but I still get t…

Why did I have problems with alembic migrations

Project structue(only directory with DB migrations):--db_manage:alembic.ini--alembic:env.pyscript.py.makoREADME--versions:#migration filesWhen I try to run command: python db_manage/alembic/env.py, I h…

Python and App Engine project structure

I am relatively new to python and app engine, and I just finished my first project. It consists of several *.py files (usually py file for every page on the site) and respectively temple files for each…

Text object in matplotlib doesnt respond to zooming properly

. Hello, everyone.I recently try to add text object in my plot. But when I zoom into the text, the text size remains unchanged. What I want to have is that the text size will increase when I zoom in an…

Python unit testing code which calls OS/Module level python functions

I have a python module/script which does a few of theseAt various nested levels inside the script I take command line inputs, validate them, apply sensible defaults I also check if a few directories ex…

How do I connect/disconnect/configure a wireless network in python?

Im looking to see if there is a way to connect or disconnect to a wireless network in python, preferably a way that would work for both public and secured networks if I supplied the password. If I can …

CSRF protection on AJAX authentication in Flask

Id like to AJAXify both a login and a signup form on a site. Up to now Ive been using WTForms mainly for its built-in CSRF protetion, but for this project I didnt feel like it was worth it -- an extra …

Pandas groupby and Multiindex

Is there any opportunity in pandas to groupby data by MultiIndex? By this i mean passing to groupby function not only keys but keys and values to predefine dataframe columns?a = np.array([foo, foo,…

How to dump YAML with explicit references?

Recursive references work great in ruamel.yaml or pyyaml: $ ruamel.yaml.dump(ruamel.yaml.load(&A [ *A ])) &id001 - *id001However it (obviously) does not work on normal references: $ ruamel.yaml…

How to set a Pydantic field value depending on other fields

from pydantic import BaseModelclass Grafana(BaseModel):user: strpassword: strhost: strport: strapi_key: str | None = NoneGRAFANA_URL = f"http://{user}:{password}@{host}:{port}"API_DATASOURCES…