feed-the-void/dd-progress-to-file.py

22 lines
621 B
Python
Raw Normal View History

#!/usr/bin/env python3
# From https://stackoverflow.com/a/25755038
from subprocess import Popen, PIPE, STDOUT
from os import remove
shell = Popen(['dd', 'if=/dev/zero', 'of=/dev/null', 'status=progress'],
stdout=PIPE, stderr=STDOUT, universal_newlines=True, bufsize=1)
remove('output')
try:
for line in iter(shell.stdout.readline, b''):
f = open('output', 'w')
if (line.strip() != ''): # Ignore empty lines (always 1 at the start)
line = line.rstrip()
print(line)
f.write(line)
f.close()
except KeyboardInterrupt:
f.close()
exit()