错误“ValueError:max()arg是一个空序列”

时间:2018-04-25 23:01:30

标签: python loops directory

我收到错误。我得到错误的原因是目录中没有更多带有.mp4的文件。我的目标是当目录中有.mp4文件时启动它,但是在找出循环时遇到问题。

File "C:\PUBG\nn.py", line 8, in <module>
    newest = max(glob.iglob('*.[Mm][Pp]4'), ke
ValueError: max() arg is an empty sequence
import os
import glob
import numpy as np
import cv2

os.chdir('C:/Users/Main/Videos/PUBG/')

newest = max(glob.iglob('*.[Mm][Pp]4'), key=os.path.getctime)

os.replace((newest), 'replay.mp4')

if newest != max(glob.iglob('*.[Mm][Pp]4'), key=os.path.getctime):
    cap = cv2.VideoCapture('C:/Users/Main/Videos/PUBG/replay.mp4')

    def rescale_frame(frame, percent=25):
        width = int(frame.shape[1] * percent/ 100)
        height = int(frame.shape[0] * percent/ 100)
        dim = (width, height)
        return cv2.resize(frame, dim, interpolation =cv2.INTER_AREA)

    while True:
        ret ,frame = cap.read()
        if type(frame) == type(None):
            break
        frame25 = rescale_frame(frame, percent=25)
        cv2.imshow('frame25',frame25)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    cap.release()
    cv2.destroyAllWindows()
os.remove('replay.mp4')

2 个答案:

答案 0 :(得分:0)

首先检查列表是否为空:

times = [os.path.getctime(p) for p in glob.iglob('*.[Mm][Pp]4')]
if not times or newest != max(times): # you might want different logic here...
    ...

答案 1 :(得分:0)

这是使用看门狗

的工作代码
import os
import glob
import numpy as np
import cv2
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

class Watcher:
    DIRECTORY_TO_WATCH = 'C:/Users/Main/Videos/PUBG/'

    def __init__(self):
        self.observer = Observer()

    def run(self):
        event_handler = Handler()
        self.observer.schedule(event_handler, self.DIRECTORY_TO_WATCH, recursive=True)
        self.observer.start()
        try:
            while True:
                time.sleep(1)
        except:
            self.observer.stop()


        self.observer.join()

class Handler(FileSystemEventHandler):

    @staticmethod
    def on_any_event(event):
        if event.is_directory:
            return None

        elif event.event_type == 'created':

            os.chdir('C:/Users/Main/Videos/PUBG/')

            newest = max(glob.iglob('*.[Mm][Pp]4'), key=os.path.getctime)

            os.replace((newest), 'replay.mp4')


            cap = cv2.VideoCapture('C:/Users/Main/Videos/PUBG/replay.mp4')

            def rescale_frame(frame, percent=25):
                width = int(frame.shape[1] * percent/ 100)
                height = int(frame.shape[0] * percent/ 100)
                dim = (width, height)
                return cv2.resize(frame, dim, interpolation =cv2.INTER_AREA)

            while True:
                ret ,frame = cap.read()
                if type(frame) == type(None):
                    break
                frame25 = rescale_frame(frame, percent=25)
                cv2.imshow('frame25',frame25)
                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break
            cap.release()
            cv2.destroyAllWindows()