[Python]如何获取目录下,最后更新的文件
#-*- coding: utf-8 -*-
__author__ = 'tsbc'
import time
import datetime
import os
day = time.strftime('%Y-%m-%d', time.localtime(time.time()))
directory = '..\\result\\'
#path要获取的文件路径
path = directory+day+"\\"
def sortfile(path):
fl = os.listdir(path) #获取当前目录文件列表
#时间戳进行倒序排序
fl.sort(key=lambda fn: os.path.getmtime(path + fn) if not os.path.isdir(path + fn) else 0)
#date.fromtimestamp(timestamp):根据给定的时间戮,返回一个date对象
dt=datetime.datetime.fromtimestamp(os.path.getmtime(path + fl[-1]))
#dt.strftime("%Y年%m月%d日 %H时%M分%S秒" 将date对象格式化显示
print('最后改动的文件是: '+fl[-1]+",时间:"+dt.strftime("%Y年%m月%d日 %H时%M分%S秒"))
[Python]如何获取目录下,最后更新的文件的更多相关文章
- python 遍历, 获取目录下所有文件名和文件夹的方法-----os.walk(), os.listdir
http://www.runoob.com/python/os-walk.html https://www.cnblogs.com/dreamer-fish/p/3820625.html 转载于:ht ...
- python生成器 获取 目录下文件
# os.walk()和os.list 都是得到所有文件的列表, 如果目录下文件特别多, 上亿了, 我们就需要生成器的方式获取 # 要求目录下面没有目录, 会递归到子目录下面找文件, (如果有子目录可 ...
- python递归获取目录下指定文件
获取一个目录下所有指定格式的文件是实际生产中常见需求. import os #递归获取一个目录下所有的指定格式的文件 def get_jsonfile(path,file_list): dir_lis ...
- python实现某目录下将多个文件夹内的文件复制到一个文件夹中
现实生活中,我们经常有这样的需求,如下图,有三个文件夹,文件夹1内含有1.txt文件 文件夹2中内含有2.txt文件,文件夹3中含有3.txt文件.我们有时候需要把1.txt, 2.txt, 3.tx ...
- python 检索一个目录下所有的txt文件,并把文件改为.log
检索一个目录及子目录下所有的txt文件,并把txt文件后缀改为log: import os f_path = r'C:\Users\PycharmProjects\mystudy\Testfolder ...
- Linux shell 获取目录下时间最新的文件的文件名
ls -lt /dirname/ | grep filename | head -n 1 |awk '{print $9}' 逐条解释: ls -lt /dirname/ 列出此目录下的所有文件并按照 ...
- 【python】获取目录下的最新文件夹/文件
直接上代码 def new_report(test_report): lists = os.listdir(test_report) #列出目录的下所有文件和文件夹保存到lists print(lis ...
- python随机选取目录下的若干个文件
个人记录用. python模块random argparse shutil import argparse parser = argparse.ArgumentParser() parser.add_ ...
- C# 获取目录下文件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
随机推荐
- mysql中字段类型转换排序
表中字段server_id是varchar类型,现在我们查询数据时想以server_id排一下序,排序后的结果 select server_id from cardserver where game_ ...
- sphinx网址
http://www.phperz.com/article/14/0615/95.htmlhttp://www.kuqin.com/shuoit/20141101/342963.htmlhttp:// ...
- eclipse汉化过程
第一步: 打开浏览器,浏览“参考资料”内给出的“eclipse语言包下载”地址,在博客新页面找到地址链接,如图所示.“Babel Language...”开头的一栏下面就是各个eclise版本的语言包 ...
- IOS第一天多线程-03线程间通信
**** #import "HMViewController.h" @interface HMViewController () @property (weak, nonatomi ...
- eval 与 Function
var json='{"name":"lee","age":"15"}' function test () { var ...
- 【7集iCore3基础视频】7-5 iTool2驱动安装
iTool2驱动安装: 高清源视频:链接:http://pan.baidu.com/s/1dF5FtlB%20密码:g5x7 iCore3 购买链接:https://item.taobao.com/i ...
- which go
http://www.yiibai.com/go/go_environment.html
- hgrjhgkjh
#include<stdio.h> int step[5]={13,5,1,4,11}; int sum; int min=999; void ji() { int i; int j; ...
- butterknife异常提示:attribute value must be constant
就是因为你的android工程是lib类型的 如: apply plugin: 'com.android.library' android { compileSdkVersion 23 buildTo ...
- Ajaxupload.js上传插件使用
注意一下火狐,360IE78下的坑: 返回过来的response在不同浏览器下的字符串不一致 // response(chrome):<pre style="word-wrap: br ...