[英]How to get the latest created file name and not the folder name using python?
[英]How to get the latest file name without path in a folder?
import glob
import os
list_of_files = glob.glob('/path/to/folder/*')
latest_file = max(list_of_files, key=os.path.getctime)
print latest_file
此代码也打印文件路径,这不是必需的。 我只需要最新的文件名。
尝试os.path.basename(latest_file)
,请参阅os :
import glob
import os
list_of_files = glob.glob('/path/to/folder/*')
latest_file = max(list_of_files, key=os.path.getctime)
print(os.path.basename(latest_file))
得到你foo.bar
list_of_files = '//x.x.x./y$/Process/Daily_Mismatch_Report/'
def get_latest_file(path, *paths): """返回连接路径的最新(最近)文件的名称""" fullpath = os.path.join(path, paths) files = glob.glob (完整路径)如果不是文件:返回 None latest_file = max(files, key=os.path.getctime) filename = os.path.split(latest_file) return filename p,f= get_latest_file(list_of_files,'. txt') print( F)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.