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 ...
随机推荐
- SPARKR,对RDD操作的介绍
(转载)SPARKR,对RDD操作的介绍 原以为,用sparkR不能做map操作, 搜了搜发现可以. lapply等同于map, 但是不能操作spark RDD. spark2.0以后, spar ...
- windows下svn钩子实现每次提交更新至web目录
目的 找 到SVN Server中的仓库(Repositories)文件夹的位置,在相应的项目文件夹中找到hooks文件夹.在该文件夹中添加一个post- commit文件:当有commit动作发生时 ...
- Java8学习笔记(四)--接口增强
增强点 静态方法 public interface InterfacePlus { void run(); static Date createDate(){ return new Date(); } ...
- 仿迅雷播放器教程 -- 基于ffmpeg的C++播放器 (1)
2011年12月份的时候发了这篇博客 http://blog.csdn.net/qq316293804/article/details/7107049 ,博文最后说会开源一个播放器,没想到快两年了,才 ...
- 如何获取控件id,包名,类名
- 【netcore基础】.Net core自动作业之Hangfire
nuget搜索:Hangfire 安装即可,这里我选择的是 1.7.0-beta1 版本 我是用这个集成到了 mvc api里 这里需要在 Startup 文件里进行如下配置 在配置方法 Config ...
- DownloadProvider源码解析——与Volley对比
1.AndroidHttpClient的创建 DownloadManager: 在DownloadThread的run方法里 public void run() { Process.setThread ...
- 【IBM-WALA】Step by Step : use WALA to generate System Dependency Graph PDF and Dot File (Mac)
Preparations: 1. IDE : eclipse (my version is luna) 2. maven (my version is 3.5.0) 3. git 4. JAVA 1. ...
- git rebase 操作撤销
git rebase可以更改提交历史,在不影响别人的情况下,能够重整git树. 但如果git rebase操作失误,却在push后才发现,怎么撤销rebase操作呢? 使用git reflog + g ...
- Python把两个列表合成一个字典
简单粗暴上代码 A= [] B = [] C= dict(map(lambda x,y:[x,y],A,B)) 酱紫,就合成了一个字典