python - 前缀文件号的零个数

标签 python

这样做的目的是找到要放入新文件名的零的数量。 比如说,如果 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/

相关文章:

python - 通过比较原因错误进行属性过滤

python - 玩弄 py2app

python - Pyglet 未绘制预定函数

python - Kafka-python 检索主题列表

python - 如何通过单个 HTTP 请求获取 Python 最新稳定版本的版本号?

python - 如果多列包含相同的数据但互换了,则从 pandas DataFrame 中删除行

python - 在 Python 中创建博客摘要?

python - "AttributeError: ' 模块 ' object has no attribute ' argv '"使用 Python.h 时

Python - 从 3D 数组写入三维图像

python - 从 QTreeWidgetItem 子类 PyQt/PySide 发出信号的替代方案