Python TypeError:stat:path应该是字符串、字节、os.PathLike或整数,而不是列表

Python TypeError:stat:path应该是字符串、字节、os.PathLike或整数,而不是列表,python,image,sorting,time,directory,Python,Image,Sorting,Time,Directory,我是python新手,我编写了一个简单的脚本,将文件夹中按时间排序的所有图像发送到API。这段代码只处理一个文件(jpg),无法发送文件夹中图像的其余部分。我想,如果我运行这段代码,它只是等待一些图像添加到当前文件夹中,当图像在文件夹中时,它将根据最初存在的图像按时间发送到API。我很困惑,任何帮助都将被感谢!thx import glob import argparse import requests import json import time import os def main():

我是python新手,我编写了一个简单的脚本,将文件夹中按时间排序的所有图像发送到API。这段代码只处理一个文件(jpg),无法发送文件夹中图像的其余部分。我想,如果我运行这段代码,它只是等待一些图像添加到当前文件夹中,当图像在文件夹中时,它将根据最初存在的图像按时间发送到API。我很困惑,任何帮助都将被感谢!thx

import glob
import argparse
import requests
import json
import time
import os

def main():
    result = []

    file = glob.glob("/path/to/dir/*.jpg")

    regions = ['id']

    time_to_wait = 10000
    time_counter = 0

    while not os.path.exists(file):
        time.sleep(1)
        time_counter += 1
        if time_counter > time_to_wait: break
        print("waiting for file...")

        if os.path.isfile(file):
            with open(file, 'rb') as fp:
                response = requests.post(
                    'https://GET_API/',
                    data=dict(regions=regions),
                    files=dict(upload=fp),
                    headers={'Authorization': 'Token ' + 'XXX'})
                result.append(response.json())
                resp_dict = json.loads(json.dumps(result, indent=2))
                if resp_dict[0]['results']:
                    num=resp_dict[0]['results'][0]['plate']
                    print(f"DETECTED NUMBER:  {num}")
                os.remove(file)

    else:
        print("file doesn't exists!")

if __name__ == '__main__':
    main()

您没有在每次迭代中更新
文件。也许这就是为什么不再检测到新文件的原因<代码>文件
也需要被视为一个列表,所以我想您应该遍历
文件
。您的while循环应该如下所示:

while True:
    files = glob.glob(os.path.join('path', 'to', 'dir', '*.jpg'))
    for file in files:
        if os.path.isfile(file):
            with open(file, 'rb') as fp:
                # Upload and delete
    # sleep

您没有在每次迭代中更新
文件。也许这就是为什么不再检测到新文件的原因<代码>文件
也需要被视为一个列表,所以我想您应该遍历
文件
。您的while循环应该如下所示:

while True:
    files = glob.glob(os.path.join('path', 'to', 'dir', '*.jpg'))
    for file in files:
        if os.path.isfile(file):
            with open(file, 'rb') as fp:
                # Upload and delete
    # sleep

非常感谢,我整天都在找这个。谢谢你!非常感谢,我整天都在找这个。谢谢你!