前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 查找文件脚本 find.py

python 查找文件脚本 find.py

作者头像
用户5760343
发布2022-05-13 11:14:56
4130
发布2022-05-13 11:14:56
举报
文章被收录于专栏:sktj

!/usr/bin/python

""" ################################################################################ Return all files matching a filename pattern at and below a root directory;

custom version of the now deprecated find module in the standard library: import as "PP4E.Tools.find"; like original, but uses os.walk loop, has no support for pruning subdirs, and is runnable as a top-level script;

find() is a generator that uses the os.walk() generator to yield just matching filenames: use findlist() to force results list generation; ################################################################################ """

import fnmatch, os

def find(pattern, startdir=os.curdir): for (thisDir, subsHere, filesHere) in os.walk(startdir): for name in subsHere + filesHere: if fnmatch.fnmatch(name, pattern): fullpath = os.path.join(thisDir, name) yield fullpath

def findlist(pattern, startdir=os.curdir, dosort=False): matches = list(find(pattern, startdir)) if dosort: matches.sort() return matches

if name == 'main': import sys namepattern, startdir = sys.argv[1], sys.argv[2] for name in find(namepattern, startdir): print(name)

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • !/usr/bin/python
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档