通过计算已处理的图像来显示for循环的进度[Python]

通过计算已处理的图像来显示for循环的进度[Python],python,for-loop,enumeration,Python,For Loop,Enumeration,我想计算已转换的图片数量。因此,我必须以某种方式修改打印“转换” 输出应该类似于。xxx应该是目录中的文件总数 如何获得此输出?而不访问您的数据,例如: def convert(self): directory = [fn for fn in os.listdir(self.destination_folder) if any(fn.endswith(ext) for ext in included_extensi

我想计算已转换的图片数量。因此,我必须以某种方式修改打印“转换”

输出应该类似于。xxx应该是目录中的文件总数


如何获得此输出?

而不访问您的数据,例如:

def convert(self):          
        directory = [fn for fn in os.listdir(self.destination_folder) 
                      if any(fn.endswith(ext) for ext in included_extensions)]

        train_length = len(directory)
        counter = 1

        for item in directory:
            if item.endswith('.jpg' ): 
                img = Image.open(self.destination_folder + item)
                pathx = self.destination_folder + item
                convert='mogrify -virtual-pixel Black +distort Plane2Cylinder 53 -crop 2060x2060+620+202 %s' %pathx
                
                subprocess.run(convert, env={'PATH': path_cur})
                print('converting', '[',counter,'/', train_length, ']')
                counter += 1

上面评论中的枚举方法更好。

你能自己演示一下解决这个问题的方法吗?对于count,enumeratedirectory中的item,1:为你循环的每个项目提供位置号。对于这种需要时间的事情,我更喜欢使用进度条,如模块
converting [1/xxx]
converting [2/xxx]
converting [3/xxx]
...
def convert(self):          
        directory = [fn for fn in os.listdir(self.destination_folder) 
                      if any(fn.endswith(ext) for ext in included_extensions)]

        train_length = len(directory)
        counter = 1

        for item in directory:
            if item.endswith('.jpg' ): 
                img = Image.open(self.destination_folder + item)
                pathx = self.destination_folder + item
                convert='mogrify -virtual-pixel Black +distort Plane2Cylinder 53 -crop 2060x2060+620+202 %s' %pathx
                
                subprocess.run(convert, env={'PATH': path_cur})
                print('converting', '[',counter,'/', train_length, ']')
                counter += 1