jwm
jwm

Reputation: 5030

python: print in the same line and overwrite the previous

I write the following code to generate a text progress bar:

for ii in range(len(imgPaths)):
    temp = misc.imread(imgPaths[ii])
    imgData[ii,:,:,:] = temp
    print 100*ii/len(imgPaths), "Percent complete\r",

The codes generate the progress as follows:

99 Percent complete Percent complete5 Percent complete7 Percent complete10 Percent complete12 Percent complete15 Percent complete17 Percent complete20 Percent complete22 Percent complete24 Percent complete27 Percent complete29 Percent complete32 Percent complete34 Percent complete37 Percent complete40 Percent complete42 Percent complete45 Percent complete48 Percent complete50 Percent complete53 Percent complete56 Percent complete58 Percent complete60 Percent complete62 Percent complete65 Percent complete67 Percent complete69 Percent complete71 Percent complete73 Percent complete76 Percent complete78 Percent complete81 Percent complete82 Percent complete84 Percent complete87 Percent complete90 Percent complete91 Percent complete94 Percent complete96 Percent complete99 Percent complete

It seems printing on the same but too redundant. Is there any way to simplify the print results in an efficient way. I want the updated progress overwrite the previous one.

Upvotes: 1

Views: 259

Answers (1)

wim
wim

Reputation: 362507

Try this instead:

print "\r{} Percent complete".format(100*ii/len(imgPaths)),

Upvotes: 1

Related Questions