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中的面向对象编程详解(上)
Apr 13 Python
Python程序中使用SQLAlchemy时出现乱码的解决方案
Apr 24 Python
Python使用filetype精确判断文件类型
Jul 02 Python
Scrapy的简单使用教程
Oct 24 Python
python利用有道翻译实现&quot;语言翻译器&quot;的功能实例
Nov 14 Python
python求解数组中两个字符串的最小距离
Sep 27 Python
python读取目录下最新的文件夹方法
Dec 24 Python
Python基于OpenCV实现人脸检测并保存
Jul 23 Python
python用match()函数爬数据方法详解
Jul 23 Python
学习和使用python的13个理由
Jul 30 Python
pytorch中如何使用DataLoader对数据集进行批处理的方法
Aug 06 Python
如何利用Python实现一个论文降重工具
Jul 09 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输出控制功能在简繁体转换中的应用
2006/10/09 PHP
PHP中的按位与和按位或操作示例
2014/01/27 PHP
php使用Cookie控制访问授权的方法
2015/01/21 PHP
php操作redis缓存方法分享
2015/06/03 PHP
PHP弹出对话框技巧详细解读
2015/09/26 PHP
分享PHP计算两个日期相差天数的代码
2015/12/23 PHP
TP5(thinkPHP5)框架基于ajax与后台数据交互操作简单示例
2018/09/03 PHP
JS宝典学习笔记(下)
2007/01/10 Javascript
JQuery扩展插件Validate—6 radio、checkbox、select的验证
2011/09/05 Javascript
JavaScript 判断用户输入的邮箱及手机格式是否正确
2013/12/08 Javascript
解决window.opener=null;window.close(),只支持IE6不支持IE7,IE8的问题
2014/01/14 Javascript
javascript中mouseover、mouseout使用详解
2015/07/19 Javascript
javascript基础知识分享之类与函数化
2016/02/13 Javascript
AngularJS入门教程之Scope(作用域)
2016/07/27 Javascript
vue 中自定义指令改变data中的值
2017/06/02 Javascript
微信小程序实现红包雨功能
2018/07/11 Javascript
深入理解JS中Number(),parseInt(),parseFloat()三者比较
2018/08/24 Javascript
Vue组件模板的几种书写形式(3种)
2020/02/19 Javascript
Vue实现附件上传功能
2020/05/28 Javascript
原生JS实现九宫格抽奖
2020/09/13 Javascript
使用Python操作Elasticsearch数据索引的教程
2015/04/08 Python
简介Django中内置的一些中间件
2015/07/24 Python
让python在hadoop上跑起来
2016/01/27 Python
Python 机器学习库 NumPy入门教程
2018/04/19 Python
使用Python处理BAM的方法
2018/09/28 Python
Python爬虫使用代理IP的实现
2019/10/27 Python
python pygame实现球球大作战
2019/11/25 Python
pymysql模块的操作实例
2019/12/17 Python
Joie官方网上商店:购买服装和女装配饰
2018/06/05 全球购物
大学奖学金获奖感言
2014/08/15 职场文书
趵突泉导游词
2015/02/03 职场文书
学校捐书倡议书
2015/04/27 职场文书
演讲比赛主持词
2015/06/29 职场文书
旅游投诉信范文
2015/07/02 职场文书
远程教育集中轮训基层干部培训班学习心得体会
2016/01/09 职场文书
用Python实现屏幕截图详解
2022/01/22 Python