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


Posted in Python onAugust 29, 2018

本文实例讲述了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实现读取命令行参数的方法
May 22 Python
Python中使用Queue和Condition进行线程同步的方法
Jan 19 Python
浅谈Python生成器generator之next和send的运行流程(详解)
May 08 Python
python+selenium识别验证码并登录的示例代码
Dec 21 Python
Python输出各行命令详解
Feb 01 Python
Python3.5.3下配置opencv3.2.0的操作方法
Apr 02 Python
python 移除字符串尾部的数字方法
Jul 17 Python
pandas读取csv文件,分隔符参数sep的实例
Dec 12 Python
基于python 微信小程序之获取已存在模板消息列表
Aug 05 Python
pycharm内无法import已安装的模块问题解决
Feb 12 Python
python GUI库图形界面开发之PyQt5布局控件QHBoxLayout详细使用方法与实例
Mar 06 Python
PyQt5爬取12306车票信息程序的实现
May 14 Python
有关Python的22个编程技巧
Aug 29 #Python
Python实现多线程的两种方式分析
Aug 29 #Python
Python运维自动化之nginx配置文件对比操作示例
Aug 29 #Python
python单例模式实例解析
Aug 28 #Python
Python3.7实现中控考勤机自动连接
Aug 28 #Python
python实现遍历文件夹修改文件后缀
Aug 28 #Python
Python绘制正余弦函数图像的方法
Aug 28 #Python
You might like
一些php项目中比较通用的php自建函数的详解
2013/06/06 PHP
PHP 实现explort() 功能的详解
2013/06/20 PHP
php 访问oracle 存储过程实例详解
2017/01/08 PHP
php实现的统计字数函数定义与使用示例
2017/07/26 PHP
ExtJs的Date格式字符代码
2010/12/30 Javascript
Javascript 面向对象编程(一) 封装
2011/08/28 Javascript
ASP.NET jQuery 实例15 通过控件CustomValidator验证CheckBoxList
2012/02/03 Javascript
jquery插件珍藏(图片局部放大/信息提示框)
2013/01/08 Javascript
实现51Map地图接口(示例代码)
2013/11/22 Javascript
Node.js中对通用模块的封装方法
2014/06/06 Javascript
jquery实现html页面 div 假分页有原理有代码
2014/09/06 Javascript
jQuery Validate初步体验(二)
2015/12/12 Javascript
JavaScript实现窗口抖动效果
2016/10/19 Javascript
原生javascript实现图片放大镜效果
2017/01/18 Javascript
JavaScript实现二分查找实例代码
2017/02/22 Javascript
利用JS实现简单的瀑布流加载图片效果
2017/04/22 Javascript
基于node.js express mvc轻量级框架实践
2017/09/14 Javascript
vue-cli的工程模板与构建工具详解
2018/09/27 Javascript
Vue触发式全局组件构建的方法
2018/11/28 Javascript
总结4个方面优化Vue项目
2019/02/11 Javascript
[03:37]2014DOTA2国际邀请赛 主赛事第一日胜者组TOPPLAY
2014/07/19 DOTA
[44:58]2018DOTA2亚洲邀请赛 4.5 淘汰赛 LGD vs Liquid 第二场
2018/04/06 DOTA
Python里disconnect UDP套接字的方法
2015/04/23 Python
python实现合并两个数组的方法
2015/05/16 Python
python学习笔记--将python源文件打包成exe文件(pyinstaller)
2018/05/26 Python
python3+selenium实现qq邮箱登陆并发送邮件功能
2019/01/23 Python
Python3实现的反转单链表算法示例
2019/03/08 Python
Scrapy-Redis之RedisSpider与RedisCrawlSpider详解
2020/11/18 Python
Django与AJAX实现网页动态数据显示的示例代码
2021/02/24 Python
Python里面如何拷贝一个对象
2014/02/17 面试题
员工辞职信范文大全
2015/05/12 职场文书
地球上的星星观后感
2015/06/02 职场文书
人与自然的观后感
2015/06/18 职场文书
SQL Server使用T-SQL语句批处理
2022/05/20 SQL Server
MySQL索引失效场景及解决方案
2022/07/23 MySQL
Java Spring读取和存储详细操作
2022/08/05 Java/Android