[英]how to retrieve the most recent file from a folder in python
I'm trying to retrieve the most recent file added or modified in a directory. 我正在尝试检索目录中添加或修改的最新文件。 The code i got on one of the forums use the method below: 我在其中一个论坛上获得的代码使用以下方法:
import os
filelist = os.listdir('MYDIRECTORY')
filelist = filter(lambda x: not os.path.isdir(x), filelist)
newest = max(filelist, key=lambda x: os.stat(x).st_mtime)
i put my directory there and it gives me the following error: 我把目录放在那里,它给了我以下错误:
Traceback (most recent call last):
File "test.py", line 8, in <module>
newest = max(filelist, key=lambda x: os.stat(x).st_mtime)
File "test.py", line 8, in <lambda>
newest = max(filelist, key=lambda x: os.stat(x).st_mtime)
OSError: [Errno 2] No such file or directory: 'NAME OF FILE'
with the name of the newest file shown above. 上面显示的最新文件的名称。 So it is finding the file, but what could be the problem. 因此,它正在查找文件,但是可能是问题所在。 Please help 请帮忙
Instead of not os.path.isdir(x)
you should use os.path.isfile(x)
. 您应该使用os.path.isfile(x)
而not os.path.isdir(x)
os.path.isfile(x)
。
filter
is deprecated; filter
已弃用; list comprehensions are preferred: 优先考虑列表:
filelist = [f for f in filelist if os.path.isfile(f)]
os.listdir
just gives you the names of the items in the directory, not full paths. os.listdir
仅提供目录中项目的名称,而不是完整路径。 To get full paths, you need to do something like os.path.join('MYDIRECTORY', f)
. 要获取完整路径,您需要执行os.path.join('MYDIRECTORY', f)
。
So all fixed up it should look like: 因此,所有修复的问题都应如下所示:
import os
rootpath = 'MYDIRECTORY'
filelist = [os.path.join(rootpath, f) for f in os.listdir(rootpath)]
filelist = [f for f in filelist if os.path.isfile(f)]
newest = max(filelist, key=lambda x: os.stat(x).st_mtime)
os.listdir()
does not include the directory you are listing in the paths that are returned. os.listdir()
在返回的路径中不包括您列出的目录。 So you either have to os.chdir("MYDIRECTORY")
into it before you call os.stat(x)
, or prepend the name of the directory like os.stat("MYDIRECTORY/" + x)
. 因此,您必须在调用os.stat(x)
之前将os.chdir("MYDIRECTORY")
放入其中,或者在目录名前加上os.stat("MYDIRECTORY/" + x)
。
Or chdir
into it beforehand and use listdir
on the current directory like os.listdir(".")
. 或预先chdir
进入目录, listdir
在当前目录中使用listdir
,例如os.listdir(".")
。
The problem is that os.listdir
returns file names , not paths . 问题是os.listdir
返回文件名 ,而不是路径 。 While it may seem that the next command (filtering on os.path.isdir
) works given just the name, you should note that isdir
returns False
when no object exists with that name. 似乎下一个命令(在os.path.isdir
过滤)仅给出名称即可工作,但应注意,当不存在具有该名称的对象时, isdir
返回False
。
What you need to do is combine the path with the file names: 您需要做的是将路径与文件名结合起来:
import os
dirname = 'MYDIRECTORY'
filenames = os.listdir(dirname)
filepaths = [os.path.join(dirname, filename) for filename in filenames]
files = [f for f in filepaths if not os.path.isdir(f)]
newest = max(files, key=lambda x: os.stat(x).st_mtime)
Note that I replaced the filter call with a list comprehension. 请注意,我用列表推导替换了filter调用。 This is considered more 'Pythonic' (eww), but I did it because list comprehensions are typically faster than filter/map in Python. 这被认为更像“ Pythonic”(eww),但我这样做是因为列表理解通常比Python中的过滤器/映射更快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.