What is the best way to get a list of all files in a directory, sorted by date [created | modified], using python, on a windows machine?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
I've done this in the past for a Python script to determine the last updated files in a directory:
That should do what you're looking for based on file mtime.
EDIT: Note that you can also use os.listdir() in place of glob.glob() if desired - the reason I used glob in my original code was that I was wanting to use glob to only search for files with a particular set of file extensions, which glob() was better suited to. To use listdir here's what it would look like:
Alex Coventry's answer will produce an exception if the file is a symlink to an unexistent file, the following code corrects that answer:
When the file doesn't exist, now() is used, and the symlink will go at the very end of the list.
Here's my answer using glob without filter if you want to read files with a certain extension in date order (Python 3).
In python 3.5+
Maybe you should use shell commands. In Unix/Linux, find piped with sort will probably be able to do what you want.
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).Example: