feed-the-void/dd-progress-to-file.py
2023-11-21 11:53:00 -06:00

25 lines
667 B
Python
Executable file

#!/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)
try:
remove('output')
except FileNotFoundError:
pass
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()