I have lots of files (about 400,000), whose identification is a six-digit number. But if the number is less than 6 digit, then we add 0's at the beginning of the number. For example, if the file identification is 25, the file name is 000025.txt. I wonder how to detect how many digits of a number and how to add the correct number of 0's at beginning of the number. Some of the code is below:
import numpy as np
fake_id = np.random.randint(0,400000,400000)
id_change = fake_id[fake_id < 100000]
#### so for fake_id < 100000, we need to find out how many digits of the id, and then we can add the correct number of zeros at the beginning.
Thanks for any help.
You can just use str.format
to "pad" with leading zeros until your number is 6 digits long
>>> '{:06d}'.format(25)
'000025'
>>> '{:06d}'.format(5432)
'005432'
>>> '{:06d}'.format(400000)
'400000'
To combine this with the rest of your task you can also use this technique to build up the file name
>>> '{:06d}.txt'.format(5432)
'005432.txt'