首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >python 查找指定目录下的指定类型文件 脚本

python 查找指定目录下的指定类型文件 脚本

作者头像
用户5760343
发布于 2022-05-13 03:09:20
发布于 2022-05-13 03:09:20
2K0
举报
文章被收录于专栏:sktjsktj

""" Find the largest file of a given type in an arbitrary directory tree. Avoid repeat paths, catch errors, add tracing and line count size. Also uses sets, file iterators and generator to avoid loading entire file, and attempts to work around undecodable dir/file name prints. """

import os, pprint from sys import argv, exc_info

trace = 1 # 0=off, 1=dirs, 2=+files dirname, extname = os.curdir, '.py' # default is .py files in cwd if len(argv) > 1: dirname = argv[1] # ex: C:, C:\Python31\Lib if len(argv) > 2: extname = argv[2] # ex: .pyw, .txt if len(argv) > 3: trace = int(argv[3]) # ex: ". .py 2"

def tryprint(arg): try: print(arg) # unprintable filename? except UnicodeEncodeError: print(arg.encode()) # try raw byte string

visited = set() allsizes = [] for (thisDir, subsHere, filesHere) in os.walk(dirname): if trace: tryprint(thisDir) thisDir = os.path.normpath(thisDir) fixname = os.path.normcase(thisDir) if fixname in visited: if trace: tryprint('skipping ' + thisDir) else: visited.add(fixname) for filename in filesHere: if filename.endswith(extname): if trace > 1: tryprint('+++' + filename) fullname = os.path.join(thisDir, filename) try: bytesize = os.path.getsize(fullname) linesize = sum(+1 for line in open(fullname, 'rb')) except Exception: print('error', exc_info()[0]) else: allsizes.append((bytesize, linesize, fullname))

for (title, key) in [('bytes', 0), ('lines', 1)]: print('\nBy %s...' % title) allsizes.sort(key=lambda x: x[key]) pprint.pprint(allsizes[:3]) pprint.pprint(allsizes[-3:])

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
python 查找多个目录下的最大Python文件 脚本
""" Find the largest Python source file on the module import search path. Skip already-visited directories, normalize path and case so they will match properly, and include line counts in pprinted result. It's not enough to use os.environ['PYTHONPATH']: this is a subset of sys.path. """
用户5760343
2022/05/13
1.5K0
python 查找目录中最大的python文件 脚本
""" Find the largest Python source file in an entire directory tree. Search the Python source lib, use pprint to display results nicely. """ import sys, os, pprint trace = False if sys.platform.startswith('win'): dirname = r'C:\Python31\Lib'
用户5760343
2022/05/13
1.1K0
python 查找文件、删除、搜索类 脚本
""" Use: "python ...\Tools\visitor_collect.py searchstring rootdir". CollectVisitor simply collects a list of all files containing a search string, for display or later processing (e.g., replacement, auto-editing); pass in a test filename extensions list to constructor to override the default in SearchVisitor; this is roughy like find+grep for text exts; """
用户5760343
2022/05/13
6870
python 目录复制类 脚本
""" Use: "python ...\Tools\visitor_cpall.py fromDir toDir trace?" Like System\Filetools\cpall.py,
用户5760343
2022/05/13
5820
python 遍历目录类 脚本
""" #################################################################################### Test: "python ...\Tools\visitor.py dir testmask [string]". Uses classes and subclasses to wrap some of the details of os.walk call usage to walk and search; testmask is an integer bitmask with 1 bit per available self-test; see also: visitor_*/.py subclasses use cases; frameworks should generally use__X pseudo private names, but all names here are exported for use in subclasses and clients; redefine reset to support multiple independent walks that require subclass updates; #################################################################################### """
用户5760343
2022/05/13
7560
python 代码总行数统计 脚本
""" Count lines among all program source files in a tree named on the command line, and report totals grouped by file types (extension). A simple SLOC (source lines of code) metric: skip blank and comment lines if desired. """
用户5760343
2022/05/13
1.1K0
python 跨平台python脚本启动器
------------------------------------调用方式-------------------------------- import Launcher Launcher.launchBookExamples(['PyDemos.pyw'], trace=False) ----------------------------------Launcher.py--------------------------------
用户5760343
2022/05/13
7560
python 比较两个目录 脚本
""" ################################################################################ Usage: "python diffall.py dir1 dir2". Recursive directory tree comparison: report unique files that exist in only dir1 or dir2, report files of the same name in dir1 and dir2 with differing contents, report instances of same name but different type in dir1 and dir2, and do the same for all subdirectories of the same names in and below dir1 and dir2. A summary of diffs appears at end of output, but search redirected output for "DIFF" and "unique" strings for further details. New: (3E) limit reads to 1M for large files, (3E) catch same name=file/dir, (4E) avoid extra os.listdir() calls in dirdiff.comparedirs() by passing results here along. ################################################################################ """
用户5760343
2022/05/13
1K0
Python os.path.help
Help on module posixpath in os: NAME posixpath - Common operations on Posix pathnames. FILE /usr/lib/python2.6/posixpath.py MODULE DOCS http://docs.python.org/library/posixpath DESCRIPTION Instead of importing this module directly, import o
py3study
2020/01/10
9530
python 查找文件脚本 search_all.py
""" ################################################################################ Use: "python ...\Tools\search_all.py dir string". Search all files at and below a named directory for a string; uses the os.walk interface, rather than doing a find.find to collect names first; similar to calling visitfile for each find.find result for "*" pattern; ################################################################################ """
用户5760343
2022/05/13
3990
python 文件分割 脚本
""" ################################################################################ split a file into a set of parts; join.py puts them back together; this is a customizable version of the standard Unix split command-line utility; because it is written in Python, it also works on Windows and can be easily modified; because it exports a function, its logic can also be imported and reused in other applications; ################################################################################ """
用户5760343
2022/05/13
9560
python 文件合并 脚本
""" ################################################################################ join all part files in a dir created by split.py, to re-create file. This is roughly like a 'cat fromdir/* > tofile' command on unix, but is more portable and configurable, and exports the join operation as a reusable function. Relies on sort order of filenames: must be same length. Could extend split/join to pop up Tkinter file selectors. ################################################################################ """
用户5760343
2022/05/13
1.2K0
python os.path模块
os.path模块主要用于文件的属性获取,在编程中经常用到,以下是该模块的几种常用方法。更多的方法可以去查看官方文档:http://docs.python.org/library/os.path.html
周小董
2019/03/25
5210
python os.path模块
语法:  os.path.join(path1[,path2[,......]])
py3study
2020/01/07
9770
偷学Python二十六|OS.path模块的详细使用说明
大家好,从本周起早起Python将持续更新由小甜同学从初学者的角度学习Python的笔记,其特点就是全文大多由新手易理解的代码与注释及动态演示。刚入门的读者千万不要错过!
刘早起
2020/06/04
9070
python3从零学习-5.4.8、fnmatch — Unix文件名模式匹配
来匹配文件名的各个部分)。 类似地,以一个句点打头的文件名也不是此模块所特有的,可以通过
用户7886150
2020/12/24
8940
python os.path模块-python快速入门教程
os.path.commonprefix(list) #返回list(多个路径)中,所有path共有的最长的路径。
jack.yang
2025/04/05
930
python 目录复制 脚本
""" ################################################################################ Usage: "python cpall.py dirFrom dirTo". Recursive copy of a directory tree. Works like a "cp -r dirFrom/* dirTo" Unix command, and assumes that dirFrom and dirTo are both directories. Was written to get around fatal error messages under Windows drag-and-drop copies (the first bad file ends the entire copy operation immediately), but also allows for coding more customized copy operations in Python. ################################################################################ """
用户5760343
2022/05/13
9020
Python3.8 了解的差不多了吧,Python3.9 新特性了解一下!
原文有删改:https://docs.python.org/3.9/whatsnew/3.9.html
用户1564362
2019/10/31
1.1K0
Python3.8 了解的差不多了吧,Python3.9 新特性了解一下!
原文有删改:https://docs.python.org/3.9/whatsnew/3.9.html
kbsc13
2019/10/24
1.8K0
相关推荐
python 查找多个目录下的最大Python文件 脚本
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档