Partially print a line then do something and then finish

This is the place for queries that don't fit in any of the other categories.

Partially print a line then do something and then finish

Postby pnelsonsr » Tue Apr 01, 2014 5:53 pm

I have a script and I tried to report the progress out to the screen like:

Code: Select all
from __future__ import print_function
print('{}--backing up {} db...{}'.format(cc,PgDbName,cx),end='')
e.run(cmd)
print('{} Done!{}'.format(cc,cx))

The problem is that it doesn't print to the screen the way I would like, it prints both print statements once run command complete. I want it to print from the first print function, then run the command (without any output to the screen), then when that is done, run the second print function. Anyone have a idea on how I can accomplish this?

First show
Code: Select all
--backing up mydb...

then when the backup is finished add the 'Done!' to the end
Code: Select all
--backing up mydb...Done!
Last edited by pnelsonsr on Tue Apr 01, 2014 6:20 pm, edited 2 times in total.
pnelsonsr
 
Posts: 28
Joined: Thu Dec 12, 2013 12:48 am

Re: Partially print a line then do something and then finish

Postby pnelsonsr » Tue Apr 01, 2014 6:00 pm

Well I was looking through sys module and I found stdout.flush() which I tried and it worked. Here is what it looks like now:

Code: Select all
from __future__ import print_function
print('{}--backing up {} db...{}'.format(cc,PgDbName,cx),end='')
sys.stdout.flush()
e.run(cmd)
print('{} Done!{}'.format(cc,cx))
pnelsonsr
 
Posts: 28
Joined: Thu Dec 12, 2013 12:48 am


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], mdovey, snippsat and 4 guests