简体   繁体   中英

Using glob.glob to pick random files gives weird errors

I'm using this method to pick random files of a certain type from a folder a user inputs, and then concentate them together in MoviePy:

 filename = glob.glob(input +"/*.mp3")
sdofjsodfj = random.choice(filename)
aclips.append(AudioFileClip(sdofjsodfj))
random.shuffle(aclips)
filename = glob.glob(input + "/*.mp4")
clipname = random.choice(filename)
lend = random.uniform(.5, VideoFileClip(clipname).duration)
end = clipname
finalclip = VideoFileClip(end).subclip(lend - .5, lend).fx(vfx.speedx, random.uniform(.54, 3)).resize((250, 250))

However, this method sometimes works, but most of the time returns this error:

IndexError: Cannot choose from an empty sequence

Is there a reason why it would be doing this?

It simply means there are no .mp4 files in the directory you globbed (you may possibly have other file types in there but not mp4 ). And so an empty list is returned as filename .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM