Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,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 删除。

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

评论
登录后参与评论
暂无评论
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档