"Parse" Python string with leading zeros

Recently, the data is being renamed. As the amount of simple numbers in the past increases, the length of the numbers is constantly adjusted, and the sorting of different reading methods is not consistent. Therefore, unifying into the form of the same length is more conducive to sorting. The three filling methods are listed below.

1、bright method

The original string is aligned on the left and zero-padded on the right

格式:str.ljust(width, '0')

>>> "123".ljust(8, "0")
'12300000.jpg'

2. rjust method

The original string is right-aligned and zero-padded on the left

格式:str.rjust(width, '0')

>>> "123".rjust(8, "0")
'00000123.jpg'

3. zfill method

Zero padding on the left

格式:str.zfill(width)

>>> "123.jpg".zfill(8)
'00000123.jpg'

4. Naming over time

If the form of number is used, there may be a problem of duplication of file names in different folders. When copying, it is often prompted whether to overwrite and replace. If it is a windows system, it is easy to handle, but for the Ubuntu system, it cannot be processed with one click. Renaming each subfile individually is cumbersome and inefficient.

Therefore, is there a naming method that does not repeat at all? time can be resolved

time.time() is a built-in function in Python that gets the timestamp of the current time (seconds from January 1, 1970 00:00:00 to the current time).
The timestamp is a floating point number such as 1605012765.3969844. The unit is seconds,

import time

name = str(time.time()).replace('.', '')

insert image description here

Guess you like

Origin blog.csdn.net/ViatorSun/article/details/130424704