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中的赋值、浅拷贝、深拷贝介绍
Mar 09 Python
在Python的web框架中中编写日志列表的教程
Apr 30 Python
对python读取CT医学图像的实例详解
Jan 24 Python
画pytorch模型图,以及参数计算的方法
Aug 17 Python
Python turtle库绘制菱形的3种方式小结
Nov 23 Python
python3 实现函数写文件路径的正确方法
Nov 27 Python
浅谈Python中的继承
Jun 19 Python
Python实现播放和录制声音的功能
Aug 12 Python
如何利用python发送邮件
Sep 26 Python
python爬取音频下载的示例代码
Oct 19 Python
celery在python爬虫中定时操作实例讲解
Nov 27 Python
Pygame Event事件模块的详细示例
Nov 17 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 防恶意刷新实现代码
2010/05/16 PHP
memcache命令启动参数中文解释
2014/01/13 PHP
ThinkPHP设置禁止百度等搜索引擎转码(简单实用)
2016/02/15 PHP
PHP根据session与cookie用户登录状态操作类的代码
2016/05/13 PHP
Windows2003下php5.4安装配置教程(Apache2.4)
2016/06/30 PHP
PHP获取ttf格式文件字体名的方法示例
2019/03/06 PHP
Jquery 数据选择插件Pickerbox使用介绍
2012/08/24 Javascript
给artDialog 5.02 增加ajax get功能详细介绍
2012/11/13 Javascript
以JSON形式将JS中Array对象数组传至后台的方法
2014/01/06 Javascript
jquery实现input输入框实时输入触发事件代码
2014/01/28 Javascript
简易的投票系统以及js刷票思路和方法
2015/04/07 Javascript
Node.js中JavaScript操作MySQL的常用方法整理
2016/03/01 Javascript
jQuery 实现ajax传入参数含有特殊字符的方法总结
2016/10/17 Javascript
Ztree新增角色和编辑角色回显问题的解决
2016/10/25 Javascript
jQuery 出现Cannot read property ‘msie’ of undefined错误的解决方法
2016/11/23 Javascript
通过js修改input、select默认字体颜色
2017/04/19 Javascript
自定义类似于jQuery UI Selectable 的Vue指令v-selectable
2017/08/23 jQuery
layer的prompt弹出框,点击回车,触发确定事件的方法
2019/09/06 Javascript
浅谈JSON5解决了JSON的两大痛点
2020/12/14 Javascript
跟老齐学Python之玩转字符串(2)
2014/09/14 Python
Python探索之静态方法和类方法的区别详解
2017/10/27 Python
Python基于Floyd算法求解最短路径距离问题实例详解
2018/05/16 Python
Python2和Python3中urllib库中urlencode的使用注意事项
2018/11/26 Python
python 实现返回一个列表中出现次数最多的元素方法
2019/06/11 Python
python3使用print打印带颜色的字符串代码实例
2019/08/22 Python
tensorflow获取预训练模型某层参数并赋值到当前网络指定层方式
2020/01/24 Python
Python HTTP下载文件并显示下载进度条功能的实现
2020/04/02 Python
Django Session和Cookie分别实现记住用户登录状态操作
2020/07/02 Python
使用pandas实现筛选出指定列值所对应的行
2020/12/13 Python
行政管理毕业生自荐信
2014/02/24 职场文书
绿化工程实施方案
2014/03/17 职场文书
成立公司计划书
2014/05/07 职场文书
心理咨询承诺书
2014/05/20 职场文书
2014年服装销售工作总结
2014/11/27 职场文书
团结主题班会
2015/08/13 职场文书
Python数据可视化之Seaborn的安装及使用
2022/04/19 Python