Derive a command from a command or script in another python script

I am python and I am trying to write a python script to which you can connect the output of a command or script.

example command | python_sript.py

In a python script, I will mainly analyze the output of the command and save it to a file.

I thought I could do this by redirecting sys.stdin to subprocess.PIPE, but that didn't work.

sys.stdin = subprocess.PIPE

Can anyone suggest how I should approach this?

Also, what happens if a command or script transmits data at a higher speed than what a python script can handle.

Note. When i use this script

import sys
data = sys.stdin.readline()
print(data)

I get it

D:\>echo "test" | tee.py
The process tried to write to a nonexistent pipe.
Traceback (most recent call last):
  File "D:\Profiles\Administrator\My Documents\Workspace\tee.py", line 3, in <mo
dule>
    data = sys.stdin.readline()
AttributeError: 'NoneType' object has no attribute 'readline'

and when i use this

import sys
data = input()
print(data)

I get it

D:\>echo "test" | tee.py
The process tried to write to a nonexistent pipe.
Traceback (most recent call last):
  File "D:\Profiles\Administrator\My Documents\Workspace\tee.py", line 3, in <mo
dule>
    data = input()
RuntimeError: input(): lost sys.stdin
+3
source share
3 answers

. () Windows python python - NT:

D:\> echo "test" | python tee.py

sys.stdin, raw_input(), fileinput.input() .

+3

, , . , , , Python script, , stdin , . , ; script , , .

Python? , , stdin stdout.

+1

1 ( ) : stdin python_script.py. .

2 ( /) : OS, .

3 ( ): . - ?

subprocess.PIPE: , Python script, . , . - .

0

Source: https://habr.com/ru/post/1775242/


All Articles