Petr Přikryl
Petr Přikryl

Reputation: 1802

Writing progress bar into file

I have subprocess which has redirected stdout and stderr to file:

file = open(self._file_path, 'w+')
pipe = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=file, stderr=file)

Subprocess is writing progress bar into stdout:

sys.stdout.write('\r{0:.2f}%'.format(float(self.counter)/self.count*100))
sys.stdout.flush()

But carriage return is not working and result is:

3.23%
6.45%
9.68%
...
100.00%

Without stdout redirection to file (using subprocess.PIPE instead) all works fine in console and I'm getting progress on the same line.

Upvotes: 1

Views: 174

Answers (1)

bindingofisaac
bindingofisaac

Reputation: 68

if you want the file to work similar to console, delete the contents of the file before writing the new output or use seek() method to go to the beginning of the line

Upvotes: 1

Related Questions