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

26 lines
667 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)
2023-11-21 11:53:00 -06:00
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()
2023-11-21 11:53:00 -06:00
#print(line)
f.write(line)
f.close()
except KeyboardInterrupt:
f.close()
exit()