I'm having a similar problem described here:
subprocess stdin buffer not flushing on newline with bufsize=1
My script is not outputting line by line when I specify bufsize=1. It's infact outputting when the buffer fills. I want to read line by line in realtime.
I've tried the below on Linux Mint and CentOS 7. test.py is what I run, it calls the 'script' file which is executable.
test.py
#!/usr/bin/python
import subprocess
import sys
process = subprocess.Popen('/root/script',bufsize=1, stdout=subprocess.PIPE, stderr=subprocess.PIPE,close_fds=True, universal_newlines=True)
for line in iter(process.stdout.readline, b''):
print line
script
#!/usr/bin/python
import time
for i in range(0,999999):
print str('This is a short line, ' + str(i) + '\n')
time.sleep(0.01)
This outputs large numbers of lines at once with pauses between the output. The first large chunk ends at line 154
However, If I make the output line much larger in the script file:
print str('This is a long line................................................................................................. ' + str(i) + '\n')
The first large chunk ends at line 66
Why is my buffer size being ignored?
Related
I am trying to use the pigz function from linux to speed up file decompression and compression. I managed to open a file with pigz using subprocess.Popen() function but after different tries, I don't manage to read the stream from Popen() make modifications on some lines, and write it directly on a new file using pigz and subprocess as well. In the end, I use the gzip.open() function from the gzip library to write the new file and the process is as slow as reading and writing directly from the gzip.open() function.
Question:
On the following code is there a way to modify the data from the output of subprocess and directly write it to a compressed file using subprocess and pigz in order to speed up the whole operation?
inputFile = "file1.txt.gz"
outputFile = "file2.txt.gz"
def pigzStream2(inputFile, outputFile):
cmd = f'pigz -dkc {inputFile} ' # -dkc: decompress, k: do not delete original file, c:Write all processed output to stdout (won't delete)
if not sys.platform.startswith("win"):
cmd = shlex.split(cmd)
res = Popen(cmd, stdout=PIPE, stdin=PIPE, bufsize=1, text=True)
with res.stdout as f_in:
with gzip.open(outputFile, 'ab') as f_out:
count = 0
while True:
count += 1
line = f_in.readline()
if line.startswith('#'):
line = f"line {count} changed"
if not line:
print(count)
break
f_out.write(line.encode())
return 0```
I'm trying to read the content of a file with python 3.8.5 but the output is empty, I don't understand what I'm doing wrong.
Here is the code:
import subprocess
import os
filename = "ls.out"
ls_command = "ls -la"
file = open(filename, "w")
subprocess.Popen(ls_command, stdout=file, shell=True)
file.close()
# So far, all is ok. The file "ls.out" is correctly created and filled with the output of "ls -la" command"
file = open(filename, "r")
for line in file:
print(line)
file.close()
The output of this script is empty, it doesn't print anything. I'm not able to see the content of ls.out.
What is not correct here ?
Popen creates a new process and launches it but returns immediately. So the end result is that you've forked your code and have both processes running at once. Your python code in executing faster than the start and finish of ls. Thus, you need to wait for the process to finish by adding a call to wait():
import subprocess
import os
filename = "ls.out"
ls_command = "ls -la"
file = open(filename, "w")
proc = subprocess.Popen(ls_command, stdout=file, shell=True)
proc.wait()
file.close()
file = open(filename, "r")
for line in file:
print(line)
file.close()
Popen merely starts the subprocess. Chances are the file is not yet populated when you open it.
If you want to wait for the Popen object to finish, you have to call its wait method, etc; but a much better and simpler solution is to use subprocess.check_call() or one of the other higher-level wrappers.
If the command prints to standard output, why don't you read it drectly?
import subprocess
import shlex
result = subprocess.run(
shlex.split(ls_command), # avoid shell=True
check=True, text=True, capture_output=True)
line = result.stdout
After searching for a long time, I still cannot find the solution to use scipy.io.wavfile.read() to read the bytes from the stdout of FFmpeg 3.3.6.
Here is the example code working perfectly. However, it needs to save a converted file to disk.
import subprocess
import scipy.io.wavfile as wavfile
command = 'ffmpeg -i in.mp3 out.wav'
subprocess.run(command)
with open('out.wav', 'rb') as wf:
rate, signal = wavfile.read(wf)
print(rate, signal)
And here is the code I try to get the FFmpeg output from stdout and load it into scipy wavfile.
import io
import subprocess
import scipy.io.wavfile as wavfile
command = 'ffmpeg -i in.mp3 -f wav -'
proc = subprocess.run(command, stdout=subprocess.PIPE)
rate, signal = wavfile.read(io.BytesIO(proc.stdout))
print(rate, signal)
Sadly, it raises a ValueError.
Traceback (most recent call last):
File ".\err.py", line 8, in <module>
rate, signal = wavfile.read(io.BytesIO(proc.stdout))
File "C:\Users\Sean Wu\AppData\Local\Programs\Python\Python36\lib\site-
packages\scipy\io\wavfile.py", line 246, in read
raise ValueError("Unexpected end of file.")
ValueError: Unexpected end of file.
Are there any methods to solve this problem?
Apparently when the output of ffmpeg is sent to stdout, the program does not fill in the RIFF chunk size of the file header. Instead, the four bytes where the chunk size should be are all 0xFF. scipy.io.wavfile.read() expects that value to be correct, so it thinks the length of the chunk is 0xFFFFFFFF bytes.
When you give ffmpeg an output file to write, it correctly fills in the RIFF chunk size, so wavfile.read() is able to read the file in that case.
A work-around for your code is to patch the RIFF chunk size manually before the data is passed to wavfile.read() via an io.BytesIO() object. Here's a modification of your script that does that. Note: I had to use command.split() for the first argument of subprocess.run(). I'm using Python 3.5.2 on Mac OS X. Also, my test file name is "mpthreetest.mp3".
import io
import subprocess
import scipy.io.wavfile as wavfile
command = 'ffmpeg -i mpthreetest.mp3 -f wav -'
proc = subprocess.run(command.split(), stdout=subprocess.PIPE)
riff_chunk_size = len(proc.stdout) - 8
# Break up the chunk size into four bytes, held in b.
q = riff_chunk_size
b = []
for i in range(4):
q, r = divmod(q, 256)
b.append(r)
# Replace bytes 4:8 in proc.stdout with the actual size of the RIFF chunk.
riff = proc.stdout[:4] + bytes(b) + proc.stdout[8:]
rate, signal = wavfile.read(io.BytesIO(riff))
print("rate:", rate)
print("len(signal):", len(signal))
print("signal min and max:", signal.min(), signal.max())
I am trying to display a output from system . But, my script produces the result only when I run it two times. Below is the script. Using subprocess.Popen at both the places does not produce any out put and same with subprocess.call.
#!/usr/bin/env python
import subprocess
import re
contr = 0
spofchk='su - dasd -c "java -jar /fisc/dasd/bin/srmclient.jar -spof_chk"'
res22 = subprocess.call("touch /tmp/logfile",shell=True,stdout=subprocess.PIPE)
fp = open("/tmp/logfile","r+")
res6 =subprocess.Popen(spofchk,shell=True,stdout=fp)
fil_list=[]
for line in fp:
line = line.strip()
fil_list.append(line)
fp.close()
for i in fil_list[2:]:
if contr % 2 == 0:
if 'no SPOF' in i:
flag=0
#print(flag)
#print(i)
else:
flag = 1
else:
continue
#Incrementing the counter by 2 so that we will only read line with spof and no SPOF
contr+=2
The child process has its own file descriptor and therefore you may close the file in the parent as soon as the child process is started.
To read the whole child process' output that is redirected to a file, wait until it exits:
import subprocess
with open('logfile', 'wb', 0) as file:
subprocess.check_call(command, stdout=file)
with open('logfile') as file:
# read file here...
If you want to consume the output while the child process is running, use PIPE:
#!/usr/bin/env python3
from subprocess import Popen, PIPE
with Popen(command, stdout=PIPE) as process, open('logfile', 'wb') as file:
for line in process.stdout: # read b'\n'-separated lines
# handle line...
# copy it to file
file.write(line)
Here's a version for older Python versions and links to fix other possible issues.
Since subprocess open a new shell , so in first time it is not possible to create the file and the file and write the output of another subprocess at the same time
.. So only solution for this is to use os. System ..
I am trying to send the results of os.popen() to an output file. Here is the code I have been trying
import os
cmd = 'dir'
fp = os.popen(cmd)
print(fp.read()) --Prints the results to the screen
res = fp.read()
fob = open('popen_output.txt','w')
fob.write(res)
fob.close()
fp.close()
The output file is just blank. The results of the command are however displayed on screen. I have also tried using Popen like this (as per the subprocess management documentation):
import subprocess
fob = Popen('dir',stdout='popen_output.txt',shell=true).stdout
As well as:
import subprocess
subprocess.Popen('dir',stdout='popen_output.txt,shell=true)
Pass a file object to stdout not a the file name as a string, you can also use check_call in place of Popen which will raise a CalledProcessError for a non-zero exit status:
with open('popen_output.txt',"w") as f:
subprocess.check_call('dir',stdout=f)
If you are on windows subprocess.check_call('dir',stdout=f, shell=True), you could also redirect using > using shell=True:
subprocess.check_call('dir > popen_output.txt',shell=True)
Ok. This got it going. Thanks for the help!
fob = open('popen_output.txt','a')
subprocess.Popen('dir',stdout=fob,shell=True)
fob.close()
This seems to be more what you'd like to do. You can process then write to file.
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
for line in process.stdout:
#processing then write line to file...
file.write(line)
If you don't want to process, then you could just do it in your subprocess call.
subprocess.run('dir > popen_output.txt', shell=true)
The problem is you are calling fp.read() twice instead of saving the results of a single fp.read() call as res, printing res, and writing the res to the output file. A file handle is stateful, so if you call read on it twice, the current position after the first call will be at the end of the file/stream, hence your blank file.
Try this (just providing the relevant changes):
fp = os.popen(cmd)
res = fp.read()
print(res)