代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Python
】
递归查找文件
作者:
biergaizi2009
/ 发布于
2012/5/14
/
968
1.输入要查找的特定目标文件扩展名,来遍历查找特定扩展名的文件,eg:python find_file.py txt 2.通过-f指定将结果写入到特定名称的文件,eg:python -f txt_result txt,否则将结果打印在控制台 3.在指定要查找的特定扩展名文件后,指定查找的根目录,eg:python find_file.py txt /
<div>!/usr/bin/python #-*-coding:utf8-*- import sys import os import argparse import pdb from pprint import pprint</div> <div>def find_path_file(specific_file,search_directory): """ result_path_filename """ result_path_filename = list() result_path_filename.extend([os.path.join(dirpath,filename) for dirpath,dirnames,filenames in os.walk(search_directory) for filename in filenames if os.path.splitext(filename)[1] == ('.' + specific_file)]) pprint(result_path_filename)</div> <div>def find_file(specific_file,search_directory): """ result_filename don't have path """ result_filename = list() os.path.walk(search_directory,lambda arg,dirname,names:result_filename.extend([i for i in names if os.path.splitext(i)[1] == ('.' + specific_file)]),()) pprint(result_filename)</div> <div>def save_result_to_file(_filename,specific_file,search_directory): """ save result to specific file """ result_path_filename = list() result_path_filename.extend([os.path.join(dirpath,filename) for dirpath,dirnames,filenames in os.walk(search_directory) for filename in filenames if os.path.splitext(filename)[1] == ('.' + specific_file)]) with open(_filename,'w') as f: f.write("\n".join(result_path_filename))</div> if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("file_suffix",help="specific the file suffix") parser.add_argument("rootdir",help="specific the root directory") parser.add_argument("-f","--file",help="record result to file") args = parser.parse_args() specific_file = args.file_suffix search_directory = args.rootdir if args.file: filename = args.file save_result_to_file(filename,specific_file,search_directory) else: # find_file(specific_file,search_directory) find_path_file(specific_file,search_directory)</div> <div></div> <div></div> <div>find_file.py</div> <div></div> <div>!/usr/bin/python #-*-coding:utf8-*- import sys import os import argparse import pdb from pprint import pprint</div> <div>def find_path_file(specific_file,search_directory): """ result_path_filename """ result_path_filename = list() result_path_filename.extend([os.path.join(dirpath,filename) for dirpath,dirnames,filenames in os.walk(search_directory) for filename in filenames if os.path.splitext(filename)[1] == ('.' + specific_file)]) pprint(result_path_filename)</div> <div>def find_file(specific_file,search_directory): """ result_filename don't have path """ result_filename = list() os.path.walk(search_directory,lambda arg,dirname,names:result_filename.extend([i for i in names if os.path.splitext(i)[1] == ('.' + specific_file)]),()) pprint(result_filename)</div> <div>def save_result_to_file(_filename,specific_file,search_directory): """ save result to specific file """ result_path_filename = list() result_path_filename.extend([os.path.join(dirpath,filename) for dirpath,dirnames,filenames in os.walk(search_directory) for filename in filenames if os.path.splitext(filename)[1] == ('.' + specific_file)]) with open(_filename,'w') as f: f.write("\n".join(result_path_filename))</div> if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("file_suffix",help="specific the file suffix") parser.add_argument("rootdir",help="specific the root directory") parser.add_argument("-f","--file",help="record result to file") args = parser.parse_args() specific_file = args.file_suffix search_directory = args.rootdir if args.file: filename = args.file save_result_to_file(filename,specific_file,search_directory) else: # find_file(specific_file,search_directory) find_path_file(specific_file,search_directory) </div>
试试其它关键字
递归查找文件
同语言下
.
比较两个图片的相似度
.
过urllib2获取带有中文参数的url内容
.
不下载获取远程图片的宽度和高度及文件大小
.
通过qrcode库生成二维码
.
通过httplib发送GET和POST请求
.
Django下解决小文件下载
.
遍历windows的所有窗口并输出窗口标题
.
根据窗口标题调用窗口
.
python 抓取搜狗指定公众号
.
pandas读取指定列
可能有用的
.
实现测量程序运行时间及cpu使用时间
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
biergaizi2009
贡献的其它代码
(
4
)
.
用N根长度相同的火柴,首尾顺次连接,摆成一个三角形
.
jQuery循环滚动展示代码
.
文件上传servlet处理代码
.
递归查找文件
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3