Sort list of files based on creation date in Python

khabi21

I've looked around and can't find a specific answer to my solution and my brain is fried at this point. I'm trying to create an mp4 video based on some .bmp files in a folder. However, I want the files ordered by earliest modified date for the video. So I'm using the oldest date modified. I found some stuff on here about using os.path.getmtime, however if I add that it's telling me it can't find the file. I'm guessing it's because the files are located on a network and not in my local path where python is installed. Here is my code. I've confirmed everything else works, so all I need is to find out how to sort the files.

import cv2
import numpy as np
import os
from os.path import isfile, join

#change this to the path where the pictures are located
pathIn= #MyPathWhichIsOnANetworkNotWherePythonIsInstalled

#input your video name & video type:
vid_name = "FirstCalPics.mp4"

#change this to the path where the video should be saved:
pathSave = #AlsoAPathWhichIsOnANetworkNotWherePythonIsInstalled

#set your fps here:
fps = 10

pathOut = pathSave + vid_name

fourcc = cv2.VideoWriter_fourcc(*'mp4v')

frame_array = []
files = [f for f in os.listdir(pathIn) if isfile(join(pathIn, f))]

#Sort files based on date modified:
files.sort(key=os.path.getmtime)   #<--- HERE'S THE ISSUE

for i in range(len(files)):
    filename=pathIn + "\\" + files[i]
    #reading each files
    img = cv2.imread(filename)
    height, width, layers = img.shape
    size = (width,height)
    
    #inserting the frames into an image array
    frame_array.append(img)
out = cv2.VideoWriter(pathOut, fourcc, fps, size)

for i in range(len(frame_array)):
    # writing to a image array
    out.write(frame_array[i])
out.release()
M Z

The reason why it says it doesn't show up as a file when you try to use just os.path.getmtime is because you're checking just path, when you also have a directory: pathIn.

You can either use join when sorting:

files.sort(key=lambda f: os.path.getmtime(join(pathIn, f)))

Or, (and the syntax depends on your version of Python) you can directly store the full file path initially:

files = [fullPath for path in os.listdir(pathIn) if isfile((fullPath := join(pathIn, f)))]

This alleviates the need for filename=pathIn + "\\" + files[i] later on in your code.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Sort files according to creation date?

Moving files to folders based on creation date and foldername Python

Git - list files by creation date

Python: List XML Files in Directory by Creation Date or number in file name

Sort objects based on the creation date of their children

Directory into an Array then sort its files by creation date

Sort files in directory by creation date (Birth)

sorting files based on its creation date in android

Copying files based on creation/modified date

Python: Sort list by date?

Python sort list of lists partially reverse based on date

python sort lists from 1 list based on date elements

Python: How to sort a list of string values based on date and time?

List files in all subdirectories by creation date

Sort the files in a list in python

List Directory of files to text file sorted by creation date but don't show creation creation date in file

How to sort files by creation date using java 1.6

Sort the elements in Python List by date in it

Sort Python list of objects by date

Sort a Python date string list

How to sort Python list with date

How to Sort List based on custom Date Dart

How to read all files of a folder based on their creation date in SSIS?

Sort python list based on length, and then based on content

How to rename a list of files in directory based on creation time on mac

Sort python list based on substring in separate list

Python: How to sort a list based on another list

python sort list based on key sorted list

Sort a list in python based on another sorted list

TOP Ranking

HotTag

Archive