python获取指定目录下特定格式的文件名
之前一直用windows下的bat脚本获取一个目录下的指定格式的文件名,如下所示:
dir *.jpg /b/s > train.set pause
十分简单,将这个bat文件放到你想要获取文件名的目录下,然后双击运行就可以将你想要保存的文件名写在train.set文件之中了。
今天由于实际需求,这个功能需要用python来实现,在网上查了一下发现,python脚本实现起来也很方便,而且也比较灵活,如下所示:
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 12 14:59:46 2016 @author: shenruixue to calculate size after filter in conv and pool
"""
import os.path, time
import exceptions
import glob
import os class TypeError (Exception):
pass
if __name__ == '__main__':
file_srx = open("train.set")#其中包含所有待计算的文件名
file_object = open('all_time.txt', 'w')
line = file_srx.readline()
diff_time_all = 0
while line:
f = line[:-1] # 除去末尾的换行符
print (f)
print ('***********************************************************')
mtime = time.ctime(os.path.getmtime(f))
ctime = time.ctime(os.path.getctime(f))
mtime_s = (os.path.getmtime(f))
ctime_s = (os.path.getctime(f))
print "Last modified : %s, last created time: %s" % (mtime, ctime)
diff_time = (int(mtime_s) - int(ctime_s))
diff_time_all = diff_time_all + diff_time
print "diff time is ", diff_time f_record = f[:-17] #根据f给f_record进行赋值,需要根据实际情况修改17的大小
print (f_record)
print ('*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*')
os.chdir(f_record)
for file_name in glob.glob("*.jpg"):
print file_name
print ('*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*') file_object.write(file_name + " time is " + str(diff_time) + "\n")
line = file_srx.readline()
print "diff_time_all is ", diff_time_all file_object.write(str(diff_time_all))
file_object.close( )
可以看到用到了os与glog两个包,很方便快捷。
可以写成一个小脚本,以后方便使用。如下:
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 12 14:59:46 2016 @author: shenruixue to calculate size after filter in conv and pool
"""
import os.path, time
import exceptions
import glob
import os
rootdir = os.getcwd()
if __name__ == '__main__':
file_object = open('name.set', 'w')
for file_name in glob.glob("*.jpg"):
print file_name
print ('*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*')
file_object.write(rootdir + file_name + "\n")
file_object.close( )
python获取指定目录下特定格式的文件名的更多相关文章
- 一个获取指定目录下一定格式的文件名称和文件修改时间并保存为文件的python脚本
摘自:http://blog.csdn.net/forandever/article/details/5711319 一个获取指定目录下一定格式的文件名称和文件修改时间并保存为文件的python脚本 ...
- python获取指定目录下所有文件名os.walk和os.listdir
python获取指定目录下所有文件名os.walk和os.listdir 觉得有用的话,欢迎一起讨论相互学习~Follow Me os.walk 返回指定路径下所有文件和子文件夹中所有文件列表 其中文 ...
- Python获取指定目录下所有子目录、所有文件名
需求 给出制定目录,通过Python获取指定目录下的所有子目录,所有(子目录下)文件名: 实现 import os def file_name(file_dir): for root, dirs, f ...
- python获取指定目录下的所有指定后缀的文件名
使用到的函数有: os.path.splitext():分离文件名与扩展名 os.path.splitext(file)[] 获得文件名 os.path.splitext(file)[] 获得文件扩展 ...
- python中获取指定目录下所有文件名列表的程序
http://blog.csdn.net/rumswell/article/details/9818001 # -*- coding: utf-8 -*-#~ #------------------- ...
- python实现指定目录下批量文件的单词计数:并发版本
在 文章 <python实现指定目录下批量文件的单词计数:串行版本>中, 总体思路是: A. 一次性获取指定目录下的所有符合条件的文件 -> B. 一次性获取所有文件的所有文件行 - ...
- C++:获取指定目录下的所有文件
1.获得指定目录下的所有文件(不搜索子文件夹) 需要包含的头文件 #include <io.h> #include <string> #include <vector&g ...
- PHP 获取指定目录下所有文件(包含子目录)
PHP 获取指定目录下所有文件(包含子目录) //glob — 寻找与模式匹配的文件路径 $filter_dir = array('CVS', 'templates_c', 'log', 'img', ...
- TDirectory.GetFileSystemEntries获取指定目录下的目录和文件
使用函数: System.IOUtils.TDirectory.GetFileSystemEntries 所有重载: class function GetFileSystemEntries(const ...
随机推荐
- JVM学习(4)——全面总结Java的GC算法和回收机制---转载自http://www.cnblogs.com/kubixuesheng/p/5208647.html
俗话说,自己写的代码,6个月后也是别人的代码……复习!复习!复习!涉及到的知识点总结如下: 一些JVM的跟踪参数的设置 Java堆的分配参数 -Xmx 和 –Xms 应该保持一个什么关系,可以让系统的 ...
- 教程:SpagoBI开源商业智能之XML Template 图表模板
SpagoBI offers a variety of widgets' examples realized with the Highcharts library, that can be divi ...
- excel 2007 无法输入中文
解决方法: 1.32位系统:找到C:\Program Files\Common Files\Microsoft Shared\OFFICE12\Office Setup Controller,将这个文 ...
- Zookeeper系列二:分布式架构详解、分布式技术详解、分布式事务
一.分布式架构详解 1.分布式发展历程 1.1 单点集中式 特点:App.DB.FileServer都部署在一台机器上.并且访问请求量较少 1.2 应用服务和数据服务拆分 特点:App.DB.Fi ...
- [Tensorflow] RNN - 03. MultiRNNCell for Digit Prediction
Ref: http://blog.csdn.net/u014595019/article/details/52759104 Time: 2min Successfully downloaded tra ...
- [GAN] How to use GAN - Meow Generator
一篇介绍GAN应用的文章.今后GAN模型学习的主要内容. 中文链接:萌物生成器:如何使用四种GAN制造猫图 原文链接:https://ajolicoeur.wordpress.com/cats/ 项目 ...
- [Laravel] 08 - Auth & Data Migration
登录注册框架 一.加载Auth模块 Step 1, 安装Auth模块 生成相关 laravel 框架内部的代码模块: $ php artisan make:auth 自动添加了路由代码 在larave ...
- [UI] 06 - jQuery
前言 From : http://www.runoob.com/jquery/jquery-intro.html Ref: jQuery 实例 一.什么是 jQuery ? jQuery是一个Java ...
- akka cluster sharding
cluster sharding 的目的在于提供一个框架,方便实现 DDD,虽然我至今也没搞明白 DDD 到底适用于是什么场合,但是 cluster sharding 却是我目前在做的一个 proje ...
- HttpWatch的Result中出现Aborted的原因分析
我们在使用HttpWatch进行Web调试的过程中有时候会看到非HTTP Status Code(状态码)的值,例如:(Aborted). (Aborted)是HttpWatch中定义的三种非HTTP ...