Nik
Nik

Reputation: 185

Python: Saving filenames incremently

I am trying to process a bunch of files and trying to save them with incremental files names.

For example the files name should be: S1-T1-C.00001.jpeg, S1-T1-C.00002.jpeg,...., S1-T1-C.03021.jpeg and so on

The code I am currently using is:

i = o
for imagePath in imagePaths:
    ....
    cv2.imwrite('S1-T1-C.%5d.jpeg' % (i),image)
    i += 1

The output file names I get are something like this: S1-T1-C. 1.jpeg, S1-T1-C. 2.jpeg, .....,S1-T1-C. 100.jpeg,..., S1-T1-C. 3021.jpeg

How can I add the zeros to the filename?

Upvotes: 0

Views: 1285

Answers (1)

Brian Cain
Brian Cain

Reputation: 14619

Add a leading zero to the format specifier: 'S1-T1-C.%05d.jpeg' % (i)

Upvotes: 6

Related Questions