[英]Python iteration through files name in range 001-100
I have a problem.我有个问题。 I need to iterate through files in directory with the following structure - filename[number1]-[number2].png .Number 1 is in range 01-10 and number 2 is in range 000-200.
我需要遍历具有以下结构的目录中的文件 -文件名 [number1]-[number2].png 。编号 1 在 01-10 范围内,编号 2 在 000-200 范围内。 Examples of files names are file02-132.png or file09-099.png .Thanks for any help.
文件名的示例是file02-132.png或file09-099.png 。感谢您的帮助。
i know how to iterate for example例如,我知道如何迭代
fnames = ['file{}.png'.format(i) for i in range(1,200)] fnames = ['file{}.png'.format(i) for i in range(1,200)]
for fname in fnames:对于 fnames 中的 fname:
#do stuff #做东西
But it doesn't work with files like this for me但它不适用于我这样的文件
You need to use a fixed width with leading zeros in the format specifier:您需要在格式说明符中使用带有前导零的固定宽度:
fnames = ['file{:03}.png'.format(i) for i in range(1, 200)]
See the Format String Syntax section in the manual.请参阅手册中的格式字符串语法部分。
here my attempt:这是我的尝试:
fnames = ['file{:02}-{:03}.png'.format(ii, i) for ii in range(0,10) for i in range(1, 200)]
for i in fnames:
print(i)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.