Python实现查询某个目录下修改时间最新的文件示例

yipeiwu_com6年前Python基础

本文实例讲述了Python实现查询某个目录下修改时间最新的文件。分享给大家供大家参考,具体如下:

通过Python脚本,查询出某个目录下修改时间最新的文件。

应用场景举例:比如有时候需要从ftp上拷贝自己刚刚上传的文件,那么这时就需要判断哪个文件的修改时间是最新的,即最后修改的文件是我们的目标文件。

直接撸代码:

# -*- coding: utf-8 -*-
import os
import shutil
def listdir(path, list_name): #传入存储的list
 for file in os.listdir(path):
  file_path = os.path.join(path, file)
  if os.path.isdir(file_path):
   listdir(file_path, list_name)
  else:
   list_name.append((file_path,os.path.getctime(file_path)))
def newestfile(target_list):
 newest_file = target_list[0]
 for i in range(len(target_list)):
  if i < (len(target_list)-1) and newest_file[1] < target_list[i+1][1]:
   newest_file = target_list[i+1]
  else:
   continue
 print('newest file is',newest_file)
 return newest_file
#p = r'C:\Users\WMB\700c-4'
p = r'C:\Users\Administrator\Desktop\img'
list = []
listdir(p, list)
new_file = newestfile(list)
print('from:',new_file[0])
print('to:',shutil.copy(new_file[0], 'C:\\Users\\Administrator\\Desktop\\img\\a.xml'))

运行结果:

('newest file is', ('C:\\Users\\Administrator\\Desktop\\img\\logo.gif', 1535508866.833419))
('from:', 'C:\\Users\\Administrator\\Desktop\\img\\logo.gif')
('to:', None)

方法说明:

def listdir(path, list_name): #传入存储的list
 for file in os.listdir(path):
  file_path = os.path.join(path, file)
  if os.path.isdir(file_path): #如果是目录,则递归执行该方法
   listdir(file_path, list_name)
  else:
    list_name.append((file_path,os.path.getctime(file_path))) #把文件路径,文件创建时间加入list中

def newestfile(target_list): #传入包含文件路径,文件创建时间的list
 newest_file = target_list[0] #冒泡算法找出时间最大的
 for i in range(len(target_list)):
  if i < (len(target_list)-1) and newest_file[1] < target_list[i+1][1]:
   newest_file = target_list[i+1]
  else:
   continue
 print('newest file is',newest_file)
 return newest_file

shutil.copy(new_file[0], 'C:\\Users\\Administrator\\Desktop\\img\\a.xml') #文件拷贝

补充:shutil.copy(source, destination)的使用说明

shutil.copy(source, destination)(这种复制形式使用的前提是必须要有 os.chdir(你要处理的路径)

source/destination 都是字符串形式的路劲,其中destination是:

  • 1、可以是一个文件的名称,则将source文件复制为新名称的destination
  • 2、可以是一个文件夹,则将source文件复制到destination中
  • 3、若这个文件夹不存在,则将source目标文件内的内容复制到destination中

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python文件与目录操作技巧汇总》、《Python文本文件操作技巧汇总》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程

希望本文所述对大家Python程序设计有所帮助。

相关文章

python定时利用QQ邮件发送天气预报的实例

python定时利用QQ邮件发送天气预报的实例

大致介绍 好久没有写博客了,正好今天有时间把前几天写的利用python定时发送QQ邮件记录一下 1、首先利用request库去请求数据,天气预报使用的是和风天气的API(www.hewe...

python利用MethodType绑定方法到类示例代码

前言 本文主要给大家介绍了关于python用MethodType绑定方法到类的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍: 对python中MethodTyp...

python中的装饰器详解

在了解装饰器的之前一定要先了解函数作为参数传递, 什么是函数内嵌,请参考我之前写的博客函数简介 因为在python里面,函数也是对象,也可以作为参数进行传递.python装饰器本质也是...

python 实现绘制整齐的表格

python 实现绘制整齐的表格

实现效果: 方法一: 1 print "+"+"-"*8+"+"+"-"*8+"+"+"-"*8+"+" 2 print "|"+" "*2+"姓名"+" "*2+"|"+" "...

示例详解Python3 or Python2 两者之间的差异

示例详解Python3 or Python2 两者之间的差异

每门编程语言在发布更新之后,主要版本之间都会发生很大的变化。 在本文中,Vinodh Kumar 通过示例解释了 Python 2 和 Python 3 之间的一些重大差异,以帮助说明语...