Liz*_*iza 111 python windows directory
获取目录中所有文件列表的最佳方法是什么,按日期排序[created | 修改],使用python,在Windows机器上?
Jay*_*Jay 133
我以前为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))
Run Code Online (Sandbox Code Playgroud)
那应该根据文件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))
Run Code Online (Sandbox Code Playgroud)
jfs*_*jfs 55
这是一个更详细@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)
Run Code Online (Sandbox Code Playgroud)
例:
$ python stat_creation_date.py
Thu Feb 11 13:31:07 2009 stat_creation_date.py
Run Code Online (Sandbox Code Playgroud)
Mig*_*rro 26
有一个os.path.getmtime函数可以提供自纪元以来的秒数,并且应该比os.stat更快.
import os
os.chdir(directory)
sorted(filter(os.path.isfile, os.listdir('.')), key=os.path.getmtime)
Run Code Online (Sandbox Code Playgroud)
efo*_*nis 22
这是我的版本:
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
Run Code Online (Sandbox Code Playgroud)
首先,我们构建一个文件名列表.isfile()用于跳过目录; 如果应该包含目录,则可以省略它.然后,我们使用修改日期作为键对列表进行就地排序.
Gre*_*ill 20
这是一个单行:
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)])
Run Code Online (Sandbox Code Playgroud)
这会调用os.listdir()来获取文件名列表,然后为每个文件名调用os.stat()来获取创建时间,然后根据创建时间进行排序.
请注意,此方法仅为每个文件调用一次os.stat(),这比为排序中的每个比较调用它更有效.
小智 15
不改变目录:
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
Run Code Online (Sandbox Code Playgroud)
din*_*s66 11
如果你想按日期顺序读取具有特定扩展名的文件(Python 3),这是我使用glob而不使用过滤器的答案.
dataset_path='/mydir/'
files = glob.glob(dataset_path+"/morepath/*.extension")
files.sort(key=os.path.getmtime)
Run Code Online (Sandbox Code Playgroud)
ign*_*ant 11
在python 3.5+
from pathlib import Path
sorted(Path('.').iterdir(), key=lambda f: f.stat().st_mtime)
Run Code Online (Sandbox Code Playgroud)
Pyg*_*irl 11
from pathlib import Path
import os
sorted(Path('./').iterdir(), key=lambda t: t.stat().st_mtime)
Run Code Online (Sandbox Code Playgroud)
或者
sorted(Path('./').iterdir(), key=os.path.getmtime)
Run Code Online (Sandbox Code Playgroud)
或者
sorted(os.scandir('./'), key=lambda t: t.stat().st_mtime)
Run Code Online (Sandbox Code Playgroud)
其中 m 时间是修改时间。
小智 6
# *** 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))
Run Code Online (Sandbox Code Playgroud)
sorted(filter(os.path.isfile, os.listdir('.')),
key=lambda p: os.stat(p).st_mtime)
Run Code Online (Sandbox Code Playgroud)
您可以使用os.walk('.').next()[-1]而不是使用 进行过滤os.path.isfile,但这会在列表中留下无效的符号链接,并且os.stat会对它们失败。
| 归档时间: |
|
| 查看次数: |
149325 次 |
| 最近记录: |