You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

解决ValueError: max() arg is an empty sequence错误及循环需求

Fix: ValueError: max() arg is an empty sequence (Wait for MP4 Files to Exist)

Hey, let's sort out this error you're hitting! The ValueError: max() arg is an empty sequence pops up because when there are no .mp4 files in your target directory, glob.iglob('*.[Mm][Pp]4') returns an empty sequence—and max() can't work with that. To make your program wait until MP4 files exist before proceeding, here's a straightforward approach with loop-based checking:

Step 1: Add a Loop to Check for MP4 Files Repeatedly

We'll wrap the file-checking logic in a loop that keeps running until it finds MP4 files. We'll also add a short sleep to avoid spamming the system with constant directory scans.

Full Working Code

import os
import glob
import numpy as np
import cv2
import time  # Required for the sleep function

# Set your target directory
target_dir = 'C:/Users/Main/Videos/PUBG/'
os.chdir(target_dir)

# Loop until MP4 files are detected
while True:
    # Get all case-insensitive MP4 files and convert to a list
    mp4_files = list(glob.iglob('*.[Mm][Pp]4'))
    if mp4_files:
        # Exit the loop once we have files to process
        break
    print("No MP4 files found in the directory. Retrying in 10 seconds...")
    time.sleep(10)  # Adjust this sleep interval to fit your needs

# Now safely retrieve the newest file
newest = max(mp4_files, key=os.path.getctime)
# Continue with your os.replace(...) logic here

Extra: Add a Timeout to Avoid Infinite Looping

If you don't want the program to run forever waiting for files, you can add a maximum retry limit to exit gracefully after a set period:

import os
import glob
import numpy as np
import cv2
import time

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

max_retries = 60  # 60 retries × 10s = 10 minutes total wait time
retry_count = 0

while retry_count < max_retries:
    mp4_files = list(glob.iglob('*.[Mm][Pp]4'))
    if mp4_files:
        break
    retry_count += 1
    print(f"No MP4 files found. Retrying in 10 seconds... ({retry_count}/{max_retries})")
    time.sleep(10)
else:
    # Runs only if all retries are used without finding files
    print("Max retries reached. Exiting program.")
    exit()

# Proceed with processing the newest file
newest = max(mp4_files, key=os.path.getctime)
os.replace(...)

Key Details to Note

  • Converting glob.iglob() to a list with list() lets us easily check if any files exist, and we can reuse the list later instead of re-scanning the directory.
  • Adjust time.sleep(10) to a shorter or longer interval based on how quickly you expect MP4 files to appear in the directory.
  • The timeout version prevents the program from running indefinitely if something goes wrong (like the directory never receives MP4 files).

内容的提问来源于stack exchange,提问作者mupperco

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:35:08