How do you get a directory listing sorted by creation date in python?
Update: to sort dirpath
's entries by modification date in Python 3:
import os
from pathlib import Path
paths = sorted(Path(dirpath).iterdir(), key=os.path.getmtime)
(put @Pygirl's answer here for greater visibility)
If you already have a list of filenames files
, then to sort it inplace by creation time on Windows (make sure that list contains absolute path):
files.sort(key=os.path.getctime)
The list of files you could get, for example, using glob
as shown in @Jay's answer.
old answer
Here's a more verbose version of @Greg Hewgill
's answer. It is the most conforming to the question requirements. It makes a distinction between creation and modification dates (at least on Windows).
#!/usr/bin/env python
from stat import S_ISREG, ST_CTIME, ST_MODE
import os, sys, time
# path to the directory (relative or absolute)
dirpath = sys.argv[1] if len(sys.argv) == 2 else r'.'
# get all entries in the directory w/ stats
entries = (os.path.join(dirpath, fn) for fn in os.listdir(dirpath))
entries = ((os.stat(path), path) for path in entries)
# leave only regular files, insert creation date
entries = ((stat[ST_CTIME], path)
for stat, path in entries if S_ISREG(stat[ST_MODE]))
#NOTE: on Windows `ST_CTIME` is a creation date
# but on Unix it could be something else
#NOTE: use `ST_MTIME` to sort by a modification date
for cdate, path in sorted(entries):
print time.ctime(cdate), os.path.basename(path)
Example:
$ python stat_creation_date.py
Thu Feb 11 13:31:07 2009 stat_creation_date.py
How to get the list of csv files in a directory sorted by creation date in Python
You could try using os.scandir:
from os import scandir
def get_sort_files(path, file_extension):
"""Return the oldest file in path with correct file extension"""
list_of_files = [(d.stat().st_ctime, d.path) for d in scandir(path) if d.is_file() and d.path.endswith(file_extension)]
return min(list_of_files)
os.scandir seems to used less calls to stat. See this post for details.
I could see much better performance on a sample folder with 5000 csv files.
Sort list of files based on creation date in Python
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.
python sort file based on created date
Here is the program with some nice printing using the format function:
import os
import time
path = os.getcwd()
def file_info(directory):
file_list = []
for i in os.listdir(directory):
a = os.stat(os.path.join(directory,i))
file_list.append([i,time.ctime(a.st_atime),time.ctime(a.st_ctime)]) #[file,most_recent_access,created]
return file_list
file_list = file_info(path)
for item in file_list:
line = "Name: {:<20} | Last Accessed: {:>20} | Date Created: {:>20}".format(item[0],item[1],item[2])
print(line)
Here is some code with a sort function being used on the accessed time. The code is not optimized but it is very readable and you should be able to understand it.
import os
import time
path = os.getcwd()
def file_info(directory,sortLastModifiedOrNaw=False):
file_list = []
currentMin = 0 #This is the variable that will track the lowest digit
for i in os.listdir(directory):
a = os.stat(os.path.join(directory,i))
if sortLastModifiedOrNaw == True: #If you would like to sort.
if a.st_atime > currentMin: #Check if this is bigger than the current minimum.
currentMin = a.st_atime #If it is we update the current minimum
#Below we append so that it ends up in the end of the list
file_list.append([i,time.ctime(a.st_atime),time.ctime(a.st_ctime)]) #[file,most_recent_access,created]
else: #If it is smaller, it should be in the front of the list so we insert it into position 0.
file_list.insert(0,[i,time.ctime(a.st_atime),time.ctime(a.st_ctime)]) #[file,most_recent_access,created]
else: #If you would not like to sort
file_list.append([i,time.ctime(a.st_atime),time.ctime(a.st_ctime)]) #[file,most_recent_access,created]
return file_list
file_list = file_info(path)
print("Unsorted Example")
for item in file_list:
line = "Name: {:<20} | Date Last Accessed: {:>20} | Date Created: {:>20}".format(item[0],item[1],item[2])
print(line)
print("\nSorted example using last modified time")
file_list = file_info(path,sortLastModifiedOrNaw=True)
for item in file_list:
line = "Name: {:<20} | Date Last Accessed: {:>20} | Date Created: {:>20}".format(item[0],item[1],item[2])
print(line)
Sample output:
Unsorted Example
Name: .idea | Date Last Accessed: Sun Jan 3 21:13:45 2016 | Date Created: Sun Jan 3 21:13:14 2016
Name: blahblah.py | Date Last Accessed: Sun Jan 3 21:13:48 2016 | Date Created: Sun Jan 3 21:13:48 2016
Name: testhoe1.py | Date Last Accessed: Sun Jan 3 19:09:57 2016 | Date Created: Sun Jan 3 18:52:06 2016
Sorted example using last modified time
Name: testhoe1.py | Date Last Accessed: Sun Jan 3 19:09:57 2016 | Date Created: Sun Jan 3 18:52:06 2016
Name: .idea | Date Last Accessed: Sun Jan 3 21:13:45 2016 | Date Created: Sun Jan 3 21:13:14 2016
Name: blahblah.py | Date Last Accessed: Sun Jan 3 21:13:48 2016 | Date Created: Sun Jan 3 21:13:48 2016
Happy optimizing! #If you change line 12 atime to ctime it will sort based on create-time.
Python: Get a list of all files and folders in a directory, the time of creation, the time of last modification. System independent solution?
I know for a fact that os.stat
functions well on both windows
and linux
.
Documentation here
However, to fit your functionality, you could do:
You can use st_atime
to access most recent access and st_ctime
for file creation time.
import os,time
def get_information(directory):
file_list = []
for i in os.listdir(directory):
a = os.stat(os.path.join(directory,i))
file_list.append([i,time.ctime(a.st_atime),time.ctime(a.st_ctime)]) #[file,most_recent_access,created]
return file_list
print get_information("/")
I'm on a mac
and I get this,
[['.dbfseventsd', 'Thu Apr 4 18:39:35 2013', 'Thu Apr 4 18:39:35 2013'], ['.DocumentRevisions-V100', 'Wed May 15 00:00:00 2013', 'Sat Apr 13 18:11:00 2013'],....]
Sorting files by date
added.sort(key=lambda x: os.stat(os.path.join(path_to_watch, x)).st_mtime)
Will sort the added
list by the last modified time of the files
Use st_ctime
instaed of st_mtime
for creation time on Windows (it doesn't mean that on other platforms).
Related Topics
How to Extract a Single Value from a JSON Response
When Do I Need to Call Mainloop in a Tkinter Application
Python Threading Multiple Bash Subprocesses
How to Run a Flask Application
Sqlite Parameter Substitution Problem
How to Highlight Text in a Tkinter Text Widget
Generate Random Numbers Summing to a Predefined Value
Temporarily Redirect Stdout/Stderr
Index a 2D Numpy Array with 2 Lists of Indices
How to Extract the N-Th Elements from a List of Tuples
Python Regex Matching Unicode Properties
Importerror: Dll Load Failed: the Specified Module Could Not Be Found
How to Plot One Line in Different Colors
Pyqt: Connecting a Signal to a Slot to Start a Background Operation
Writing a Connection String When Password Contains Special Characters
How to Split a String of Space Separated Numbers into Integers
Convert Python Sequence to Numpy Array, Filling Missing Values
Calling a Python Script with Input Within a Python Script Using Subprocess