Python遍历目录中的2000个图像,返回最低数量

Python遍历目录中的2000个图像,返回最低数量,python,Python,下面的代码扫描文件夹中的图像,并选择最接近0的文件 但是e只捕捉到前30个,就像我有一个img_50.png脚本不会捕捉到它一样。最好返回2000年以前的图像,如img_2000.png 不确定哪里出了问题,我使用的是os.listdir,但它似乎没有抓取超过30张图像 e = 0 for image_file in os.listdir(folder_path + local_username + "/"): print("in for loop"

下面的代码扫描文件夹中的图像,并选择最接近0的文件

但是
e
只捕捉到前30个,就像我有一个
img_50.png
脚本不会捕捉到它一样。最好返回2000年以前的图像,如
img_2000.png

不确定哪里出了问题,我使用的是
os.listdir
,但它似乎没有抓取超过30张图像

e = 0
for image_file in os.listdir(folder_path + local_username + "/"):
    print("in for loop")
    try:
        print("Hit try")
        fileInput.send_keys(folder_path + local_username + "/img_" + str(e) + ".png")
        print("Selected image from folder")
    except:
        e += 1
        print("Hit except")
它会打印出来,只会重复两次

in for loop
Hit try
Hit except
in for loop
Hit try
Hit except
原创的

e = 0
for image_file in os.listdir(folder_path + local_username + "/"):
    print("in for loop")
    try:
        print("Hit try")
        fileInput.send_keys(folder_path + local_username + "/img_" + str(e) + ".png")
        print("Selected image from folder")
    except:
        e += 1
        print("Hit except")
这起到了作用:

e = 0
for image_file in range(0,2000):
    print("in for loop")
    try:
        print("Hit try")
        fileInput.send_keys(folder_path + local_username + "/img_" + str(e) + ".png")
        print("Selected image from folder")
    except:
        e += 1
        print("Hit except")

您似乎只扫描/检查了一个文件夹中的图像,这个文件夹有多少图像?@sai 2000 images为什么不使用
Path(文件夹路径+本地用户名+“/”).glob('*.png')
,这将为您提供一个生成器,您可以迭代读取所有图像。您不应该在“try”块中也增加e吗?@sai`用于glob.iglob(文件夹路径+本地用户名+“/”).glob('*.png'):AttributeError:'generator'对象没有属性“glob”`很好,它正在工作,但是为了努力成为一个好的人而改进会更好!对于初学者来说,如果要添加更多图像,现在的代码将无法灵活处理。其次,except块应该处理通常可预见的错误,例如在这种特殊情况下的“FileNotFoundError”。最后,
Path(folder_Path+local_username+“/”).glob('*.png')
,如果您不知道此模块,
Path
pathlib
包的模块部分,可以通过从pathlib导入路径导入类似的
来使用