这样做的目的是找到要放入新文件名的零的数量。 比如说,如果 dir 中有 86 个文件,我们需要从 00(或 01)开始文件名,如果有 600、001 等。
dir_ls = os.listdir(os.getcwd())
# number of zeroes to prefix file number
ln = len(str(len(dir_ls))) - 1
这样可以吗,或者可以做得更好吗? 这种双重转换感觉很愚蠢。
最佳答案
>>> import math
>>> int(math.ceil(math.log10(86)))
2
>>> int(math.ceil(math.log10(600)))
3
关于python - 前缀文件号的零个数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8501136/