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

发布于 2024-07-07 18:53:54 字数 67 浏览 3 评论 0原文

获取目录中所有文件的列表(按日期排序)的最佳方法是什么[创建| 修改],在 Windows 机器上使用 python?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(19

小苏打饼 2024-07-14 18:53:54

我过去曾使用 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 = list(filter(os.path.isfile, glob.glob(search_dir + "*")))
files.sort(key=lambda x: os.path.getmtime(x))

这应该可以根据文件 mtime 执行您要查找的操作。

编辑:请注意,如果需要,您还可以使用 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))

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

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 = list(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.

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:

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))
我要还你自由 2024-07-14 18:53:54

更新:在 Python 3 中按修改日期对 dirpath 的条目进行排序:

import os
from pathlib import Path

paths = sorted(Path(dirpath).iterdir(), key=os.path.getmtime)

(put @Pygirl 的回答在这里以获得更好的可见性)

如果您已经有一个文件名文件列表,那么在 Windows 上按创建时间对其进行排序(确保该列表包含绝对路径):

files.sort(key=os.path.getctime)

您可以获得的文件列表,例如,使用 glob ,如 中所示@Jay 的回答


旧答案
这是 @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)

例子:

$ python stat_creation_date.py
Thu Feb 11 13:31:07 2009 stat_creation_date.py

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
陌伤ぢ 2024-07-14 18:53:54

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

import os 

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

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

import os 

os.chdir(directory)
sorted(filter(os.path.isfile, os.listdir('.')), key=os.path.getmtime)
伤痕我心 2024-07-14 18:53:54

这是我的版本:

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

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

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.

童话里做英雄 2024-07-14 18:53:54

这是一个单行代码:

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

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

请注意,此方法仅对每个文件调用 os.stat() 一次,这比为排序中的每个比较调用它更有效。

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.

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.

‘画卷フ 2024-07-14 18:53:54

在Python 3.5+中

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

In python 3.5+

from pathlib import Path
sorted(Path('.').iterdir(), key=lambda f: f.stat().st_mtime)
笑脸一如从前 2024-07-14 18:53:54

不改变目录:

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

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
过度放纵 2024-07-14 18:53:54
from pathlib import Path
import os

sorted(Path('./').iterdir(), key=lambda t: t.stat().st_mtime)

sorted(Path('./').iterdir(), key=os.path.getmtime)

sorted(os.scandir('./'), key=lambda t: t.stat().st_mtime)

其中 m 时间是修改时间。

from pathlib import Path
import os

sorted(Path('./').iterdir(), key=lambda t: t.stat().st_mtime)

or

sorted(Path('./').iterdir(), key=os.path.getmtime)

or

sorted(os.scandir('./'), key=lambda t: t.stat().st_mtime)

where m time is modified time.

迷爱 2024-07-14 18:53:54

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

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

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

dataset_path='/mydir/'   
files = glob.glob(dataset_path+"/morepath/*.extension")   
files.sort(key=os.path.getmtime)
十雾 2024-07-14 18:53:54
# *** the shortest and best way ***
# getmtime --> sort by modified time
# getctime --> sort by created time

import glob,os

lst_files = glob.glob("*.txt")
lst_files.sort(key=os.path.getmtime)
print("\n".join(lst_files))
# *** the shortest and best way ***
# getmtime --> sort by modified time
# getctime --> sort by created time

import glob,os

lst_files = glob.glob("*.txt")
lst_files.sort(key=os.path.getmtime)
print("\n".join(lst_files))
‖放下 2024-07-14 18:53:54
sorted(filter(os.path.isfile, os.listdir('.')), 
    key=lambda p: os.stat(p).st_mtime)

您可以使用 os.walk('.').next()[-1] 而不是使用 os.path.isfile 进行过滤,但这会在列表,并且 os.stat 将失败。

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.

简单气质女生网名 2024-07-14 18:53:54

为了保证 os.scandir 的完整性(比 pathlib 快 2 倍):

import os
sorted(os.scandir('/tmp/test'), key=lambda d: d.stat().st_mtime)

For completeness with os.scandir (2x faster over pathlib):

import os
sorted(os.scandir('/tmp/test'), key=lambda d: d.stat().st_mtime)
若水微香 2024-07-14 18:53:54

这是学习的基本步骤:

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

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
破晓 2024-07-14 18:53:54

如果文件是指向不存在文件的符号链接,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())

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

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

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.

无声无音无过去 2024-07-14 18:53:54

这是我的版本:

import os

folder_path = r'D:\Movies\extra\new\dramas' # your path
os.chdir(folder_path) # make the path active
x = sorted(os.listdir(), key=os.path.getctime)  # sorted using creation time

folder = 0

for folder in range(len(x)):
    print(x[folder]) # print all the foldername inside the folder_path
    folder = +1

This was my version:

import os

folder_path = r'D:\Movies\extra\new\dramas' # your path
os.chdir(folder_path) # make the path active
x = sorted(os.listdir(), key=os.path.getctime)  # sorted using creation time

folder = 0

for folder in range(len(x)):
    print(x[folder]) # print all the foldername inside the folder_path
    folder = +1
素手挽清风 2024-07-14 18:53:54

这是几行简单的代码,用于查找扩展名并提供排序选项

def get_sorted_files(src_dir, regex_ext='*', sort_reverse=False): 
    files_to_evaluate = [os.path.join(src_dir, f) for f in os.listdir(src_dir) if re.search(r'.*\.({})
.format(regex_ext), f)]
    files_to_evaluate.sort(key=os.path.getmtime, reverse=sort_reverse)
    return files_to_evaluate

Here is a simple couple lines that looks for extention as well as provides a sort option

def get_sorted_files(src_dir, regex_ext='*', sort_reverse=False): 
    files_to_evaluate = [os.path.join(src_dir, f) for f in os.listdir(src_dir) if re.search(r'.*\.({})
.format(regex_ext), f)]
    files_to_evaluate.sort(key=os.path.getmtime, reverse=sort_reverse)
    return files_to_evaluate
只怪假的太真实 2024-07-14 18:53:54

在路径中添加文件目录/文件夹,如果想要特定的文件类型,请添加文件扩展名,然后按时间顺序获取文件名。
这对我有用。

import glob, os
from pathlib import Path
path = os.path.expanduser(file_location+"/"+date_file)  
os.chdir(path)    
saved_file=glob.glob('*.xlsx')
saved_file.sort(key=os.path.getmtime)

print(saved_file)

Add the file directory/folder in path, if you want to have specific file type add the file extension, and then get file name in chronological order.
This works for me.

import glob, os
from pathlib import Path
path = os.path.expanduser(file_location+"/"+date_file)  
os.chdir(path)    
saved_file=glob.glob('*.xlsx')
saved_file.sort(key=os.path.getmtime)

print(saved_file)
终难遇 2024-07-14 18:53:54

结果 os.listdir 按上次修改顺序排序,但相反,因此您可以执行以下操作:

import os
last_modified=os.listdir()[::-1]

Turns out os.listdir sorts by last modified but in reverse so you can do:

import os
last_modified=os.listdir()[::-1]
灯角 2024-07-14 18:53:54

也许你应该使用 shell 命令。 在 Unix/Linux 中,find pipelined 和 sort 可能可以做你想做的事情。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文