前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >python 查找多个目录下的最大Python文件 脚本

python 查找多个目录下的最大Python文件 脚本

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

""" 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. """

import sys, os, pprint trace = 0 # 1=dirs, 2=+files

visited = {} allsizes = [] for srcdir in sys.path: for (thisDir, subsHere, filesHere) in os.walk(srcdir): if trace > 0: print(thisDir) thisDir = os.path.normpath(thisDir) fixcase = os.path.normcase(thisDir) if fixcase in visited: continue else: visited[fixcase] = True for filename in filesHere: if filename.endswith('.py'): if trace > 1: print('...', filename) pypath = os.path.join(thisDir, filename) try: pysize = os.path.getsize(pypath) except os.error: print('skipping', pypath, sys.exc_info()[0]) else: pylines = len(open(pypath, 'rb').readlines()) allsizes.append((pysize, pylines, pypath))

print('By size...') allsizes.sort() pprint.pprint(allsizes[:3]) pprint.pprint(allsizes[-3:])

print('By lines...') allsizes.sort(key=lambda x: x[1]) pprint.pprint(allsizes[:3]) pprint.pprint(allsizes[-3:])

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

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

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

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

评论
登录后参与评论
暂无评论
推荐阅读
python 查找指定目录下的指定类型文件 脚本
""" 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. """
用户5760343
2022/05/13
1.9K0
python 跨平台python脚本启动器
------------------------------------调用方式-------------------------------- import Launcher Launcher.launchBookExamples(['PyDemos.pyw'], trace=False) ----------------------------------Launcher.py--------------------------------
用户5760343
2022/05/13
7030
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
6420
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模块学习
系统相关的信息模块: import sys sys.argv 是一个 list,包含所有的命令行参数. sys.stdout sys.stdin sys.stderr 分别表示标准输入输出,错误输出的文件对象. sys.stdin.readline() 从标准输入读一行 sys.stdout.write("a") 屏幕输出a sys.exit(exit_code) 退出程序 sys.modules 是一个dictionary,表示系统中所有可用的module sys.
py3study
2020/01/13
3730
Python里的OS与SYS
os.name字符串指示你正在使用的平台。比如对于Windows,它是'nt',而对于Linux/Unix用户,它是'posix'。
我被狗咬了
2019/09/23
7120
Python 【os.path()模块解析】
实例6:os.path.getatime/os.path.getmtime/os.path.getctime
IT茂茂
2020/10/26
8770
偷学Python二十六|OS.path模块的详细使用说明
大家好,从本周起早起Python将持续更新由小甜同学从初学者的角度学习Python的笔记,其特点就是全文大多由新手易理解的代码与注释及动态演示。刚入门的读者千万不要错过!
刘早起
2020/06/04
8880
python 自定义launch脚本
""" ################################################################################### launch Python programs with command lines and reusable launcher scheme classes; auto inserts "python" and/or path to Python executable at front of command line; som
用户5760343
2022/05/13
3520
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
6990
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
9270
python os.path模块
语法:  os.path.join(path1[,path2[,......]])
py3study
2020/01/07
9480
Python常用方法(上)
#1.生成器 range(开头,结尾,步长) range(5,-1,-1),可以逐步减少 id(xx) #打印唯一ID值 enumerate([1,2,3], 1): #配合循环使用,列表和序号,右侧指定序号初始位,需要2个变量承接 #2.随机数 random.randint(1, 10) #1-10随机 random.shuffle(xx) #对列表随机排序 random.sample(xx,3) #从列表随机抽出3个 #3.sys模块 sys.exit() #退出 sys.argv[1] #位置变量,一
陈不成i
2021/06/21
3340
python os.path模块
os.path模块主要用于文件的属性获取,在编程中经常用到,以下是该模块的几种常用方法。更多的方法可以去查看官方文档:http://docs.python.org/library/os.path.html
周小董
2019/03/25
5010
python 目录复制类 脚本
""" Use: "python ...\Tools\visitor_cpall.py fromDir toDir trace?" Like System\Filetools\cpall.py,
用户5760343
2022/05/13
5510
nginx切割日志脚本(python)
因为以前没有做nginx日志分割,有时候想看日志的时候总是发现有十几G的甚至上百G的日志文件,于是就想使用python写个nginx日志分割(当然你也可以使用shell来完成都是很简单)
py3study
2020/01/07
8420
[快学Python3]目录与文件操作
概述 本文就os和path模块中常用的方法进行了说明和列举,更多的方法和技巧请参加官方文档 os模块常用方法 我们先看看os模块所提供的目录操作方法,直接上代码实例: # -*- coding:utf-8 -*- __author__ = '苦叶子' # 导入os模块 import os if __name__ == "__main__": # 返回完整的路径目录 print("获取当前工作目录") print(os.getcwd()) # 返
苦叶子
2018/04/09
1.8K0
python guimixin 消息调用 工具dialog封装
""" ############################################################################### a "mixin" class for other frames: common methods for canned dialogs, spawning programs, simple text viewers, etc; this class must be mixed with a Frame (or a subclass derived from Frame) for its quit method ############################################################################### """
用户5760343
2022/05/13
3290
Python中os与sys两模块的区别 原
os: This module provides a portable way of using operating system dependent functionality.
晓歌
2018/08/15
5360
相关推荐
python 查找指定目录下的指定类型文件 脚本
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档