如何在python中按创建日期排序目录列表?

时间:2021-04-20 00:12:20

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?

获得一个目录中所有文件的列表的最佳方式是什么?按照日期[创建|修改],使用python,在windows机器上进行排序。

12 个解决方案

#1


46  

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).

下面是一个更加详细的@Greg Hewgill的回答。它是最符合问题需求的。它区分了创建日期和修改日期(至少在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

#2


90  

I've done this in the past for a Python script to determine the last updated files in a directory:

我曾经在Python脚本中这样做过,以确定目录中最近更新的文件:

import glob
import os

search_dir = "/mydir/"
# remove anything from the list that is not a file (directories, symlinks)
# thanks to J.F. Sebastion for pointing out that the requirement was a list 
# of files (presumably not including directories)  
files = filter(os.path.isfile, glob.glob(search_dir + "*"))
files.sort(key=lambda x: os.path.getmtime(x))

That should do what you're looking for based on file mtime.

这应该是基于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:

编辑:注意,如果需要,您也可以使用os.listdir()代替glob.glob()——我在原始代码中使用glob的原因是,我希望使用glob只搜索具有特定文件扩展集的文件,glob()更适合这种扩展集。使用listdir如下:

import os

search_dir = "/mydir/"
os.chdir(search_dir)
files = filter(os.path.isfile, os.listdir(search_dir))
files = [os.path.join(search_dir, f) for f in files] # add path to each file
files.sort(key=lambda x: os.path.getmtime(x))

#3


19  

Here's a one-liner:

这里有一个小笑话:

import os
import time
from pprint import pprint

pprint([(x[0], time.ctime(x[1].st_ctime)) for x in sorted([(fn, os.stat(fn)) for fn in os.listdir(".")], key = lambda x: x[1].st_ctime)])

This calls os.listdir() to get a list of the filenames, then calls os.stat() for each one to get the creation time, then sorts against the creation time.

它调用os.listdir()来获取文件名的列表,然后为每个文件名调用os.stat()来获取创建时间,然后对创建时间进行排序。

Note that this method only calls os.stat() once for each file, which will be more efficient than calling it for each comparison in a sort.

注意,该方法对每个文件只调用一次os.stat(),这比对每个排序中的比较调用它要高效得多。

#4


17  

Here's my version:

这是我的版本:

def getfiles(dirpath):
    a = [s for s in os.listdir(dirpath)
         if os.path.isfile(os.path.join(dirpath, s))]
    a.sort(key=lambda s: os.path.getmtime(os.path.join(dirpath, s)))
    return a

First, we build a list of the file names. isfile() is used to skip directories; it can be omitted if directories should be included. Then, we sort the list in-place, using the modify date as the key.

首先,我们构建一个文件名列表。isfile()用于跳过目录;如果应该包含目录,可以省略它。然后,我们使用修改日期作为键对列表进行就地排序。

#5


14  

There is an os.path.getmtime function that gives the number of seconds since the epoch and should be faster than os.stat.

有一个os.path。getmtime函数,给出自纪元以来的秒数,并且应该比os.stat快。

os.chdir(directory)
sorted(filter(os.path.isfile, os.listdir('.')), key=os.path.getmtime)

#6


14  

Without changing directory:

在不改变目录:

import os    

path = '/path/to/files/'
name_list = os.listdir(path)
full_list = [os.path.join(path,i) for i in name_list]
time_sorted_list = sorted(full_list, key=os.path.getmtime)

print time_sorted_list

# if you want just the filenames sorted, simply remove the dir from each
sorted_filename_list = [ os.path.basename(i) for i in time_sorted_list]
print sorted_filename_list

#7


8  

Here's my answer using glob without filter if you want to read files with a certain extension in date order (Python 3).

如果您希望按日期顺序读取具有特定扩展名的文件(Python 3),我的答案是使用没有过滤器的glob。

dataset_path='/mydir/'   
files = glob.glob(dataset_path+"/morepath/*.extension")   
files.sort(key=os.path.getmtime)

#8


4  

sorted(filter(os.path.isfile, os.listdir('.')), 
    key=lambda p: os.stat(p).st_mtime)

You could use os.walk('.').next()[-1] instead of filtering with os.path.isfile, but that leaves dead symlinks in the list, and os.stat will fail on them.

您可以使用os.walk('. .').next()[-1]而不是使用os.path进行过滤。isfile,但是在列表中会留下死的符号链接和os。他们会失败的。

#9


3  

In python 3.5+

在python 3.5 +

from pathlib import Path
sorted(Path('.').iterdir(), key=lambda f: f.stat().st_mtime)

#10


1  

this is a basic step for learn:

这是学习的基本步骤:

import os, stat, sys
import time

dirpath = sys.argv[1] if len(sys.argv) == 2 else r'.'

listdir = os.listdir(dirpath)

for i in listdir:
    os.chdir(dirpath)
    data_001 = os.path.realpath(i)
    listdir_stat1 = os.stat(data_001)
    listdir_stat2 = ((os.stat(data_001), data_001))
    print time.ctime(listdir_stat1.st_ctime), data_001

#11


1  

Alex Coventry's answer will produce an exception if the file is a symlink to an unexistent file, the following code corrects that answer:

Alex Coventry的答案将产生一个异常,如果该文件是一个不存在的文件的符号链接,下面的代码将纠正这个答案:

import time
import datetime
sorted(filter(os.path.isfile, os.listdir('.')), 
    key=lambda p: os.path.exists(p) and os.stat(p).st_mtime or time.mktime(datetime.now().timetuple())

When the file doesn't exist, now() is used, and the symlink will go at the very end of the list.

当文件不存在时,使用now(),符号链接将位于列表的末尾。

#12


-4  

Maybe you should use shell commands. In Unix/Linux, find piped with sort will probably be able to do what you want.

也许您应该使用shell命令。在Unix/Linux中,查找带有sort的管道可能可以做您想做的事情。

#1


46  

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).

下面是一个更加详细的@Greg Hewgill的回答。它是最符合问题需求的。它区分了创建日期和修改日期(至少在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

#2


90  

I've done this in the past for a Python script to determine the last updated files in a directory:

我曾经在Python脚本中这样做过,以确定目录中最近更新的文件:

import glob
import os

search_dir = "/mydir/"
# remove anything from the list that is not a file (directories, symlinks)
# thanks to J.F. Sebastion for pointing out that the requirement was a list 
# of files (presumably not including directories)  
files = filter(os.path.isfile, glob.glob(search_dir + "*"))
files.sort(key=lambda x: os.path.getmtime(x))

That should do what you're looking for based on file mtime.

这应该是基于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:

编辑:注意,如果需要,您也可以使用os.listdir()代替glob.glob()——我在原始代码中使用glob的原因是,我希望使用glob只搜索具有特定文件扩展集的文件,glob()更适合这种扩展集。使用listdir如下:

import os

search_dir = "/mydir/"
os.chdir(search_dir)
files = filter(os.path.isfile, os.listdir(search_dir))
files = [os.path.join(search_dir, f) for f in files] # add path to each file
files.sort(key=lambda x: os.path.getmtime(x))

#3


19  

Here's a one-liner:

这里有一个小笑话:

import os
import time
from pprint import pprint

pprint([(x[0], time.ctime(x[1].st_ctime)) for x in sorted([(fn, os.stat(fn)) for fn in os.listdir(".")], key = lambda x: x[1].st_ctime)])

This calls os.listdir() to get a list of the filenames, then calls os.stat() for each one to get the creation time, then sorts against the creation time.

它调用os.listdir()来获取文件名的列表,然后为每个文件名调用os.stat()来获取创建时间,然后对创建时间进行排序。

Note that this method only calls os.stat() once for each file, which will be more efficient than calling it for each comparison in a sort.

注意,该方法对每个文件只调用一次os.stat(),这比对每个排序中的比较调用它要高效得多。

#4


17  

Here's my version:

这是我的版本:

def getfiles(dirpath):
    a = [s for s in os.listdir(dirpath)
         if os.path.isfile(os.path.join(dirpath, s))]
    a.sort(key=lambda s: os.path.getmtime(os.path.join(dirpath, s)))
    return a

First, we build a list of the file names. isfile() is used to skip directories; it can be omitted if directories should be included. Then, we sort the list in-place, using the modify date as the key.

首先,我们构建一个文件名列表。isfile()用于跳过目录;如果应该包含目录,可以省略它。然后,我们使用修改日期作为键对列表进行就地排序。

#5


14  

There is an os.path.getmtime function that gives the number of seconds since the epoch and should be faster than os.stat.

有一个os.path。getmtime函数,给出自纪元以来的秒数,并且应该比os.stat快。

os.chdir(directory)
sorted(filter(os.path.isfile, os.listdir('.')), key=os.path.getmtime)

#6


14  

Without changing directory:

在不改变目录:

import os    

path = '/path/to/files/'
name_list = os.listdir(path)
full_list = [os.path.join(path,i) for i in name_list]
time_sorted_list = sorted(full_list, key=os.path.getmtime)

print time_sorted_list

# if you want just the filenames sorted, simply remove the dir from each
sorted_filename_list = [ os.path.basename(i) for i in time_sorted_list]
print sorted_filename_list

#7


8  

Here's my answer using glob without filter if you want to read files with a certain extension in date order (Python 3).

如果您希望按日期顺序读取具有特定扩展名的文件(Python 3),我的答案是使用没有过滤器的glob。

dataset_path='/mydir/'   
files = glob.glob(dataset_path+"/morepath/*.extension")   
files.sort(key=os.path.getmtime)

#8


4  

sorted(filter(os.path.isfile, os.listdir('.')), 
    key=lambda p: os.stat(p).st_mtime)

You could use os.walk('.').next()[-1] instead of filtering with os.path.isfile, but that leaves dead symlinks in the list, and os.stat will fail on them.

您可以使用os.walk('. .').next()[-1]而不是使用os.path进行过滤。isfile,但是在列表中会留下死的符号链接和os。他们会失败的。

#9


3  

In python 3.5+

在python 3.5 +

from pathlib import Path
sorted(Path('.').iterdir(), key=lambda f: f.stat().st_mtime)

#10


1  

this is a basic step for learn:

这是学习的基本步骤:

import os, stat, sys
import time

dirpath = sys.argv[1] if len(sys.argv) == 2 else r'.'

listdir = os.listdir(dirpath)

for i in listdir:
    os.chdir(dirpath)
    data_001 = os.path.realpath(i)
    listdir_stat1 = os.stat(data_001)
    listdir_stat2 = ((os.stat(data_001), data_001))
    print time.ctime(listdir_stat1.st_ctime), data_001

#11


1  

Alex Coventry's answer will produce an exception if the file is a symlink to an unexistent file, the following code corrects that answer:

Alex Coventry的答案将产生一个异常,如果该文件是一个不存在的文件的符号链接,下面的代码将纠正这个答案:

import time
import datetime
sorted(filter(os.path.isfile, os.listdir('.')), 
    key=lambda p: os.path.exists(p) and os.stat(p).st_mtime or time.mktime(datetime.now().timetuple())

When the file doesn't exist, now() is used, and the symlink will go at the very end of the list.

当文件不存在时,使用now(),符号链接将位于列表的末尾。

#12


-4  

Maybe you should use shell commands. In Unix/Linux, find piped with sort will probably be able to do what you want.

也许您应该使用shell命令。在Unix/Linux中,查找带有sort的管道可能可以做您想做的事情。