Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >python 查找文件、删除、搜索类 脚本

python 查找文件、删除、搜索类 脚本

作者头像
用户5760343
发布于 2022-05-13 03:03:30
发布于 2022-05-13 03:03:30
65500
代码可运行
举报
文章被收录于专栏:sktjsktj
运行总次数:0
代码可运行

搜集

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

import sys from visitor import SearchVisitor

class CollectVisitor(SearchVisitor): """ collect names of files containing a string; run this and then fetch its obj.matches list """ def init(self, searchstr, testexts=None, trace=1): self.matches = [] if testexts != None: self.testexts = testexts SearchVisitor.init(self, searchstr, trace) def visitmatch(self, fname, text): self.matches.append(fname)

if name == 'main': visitor = CollectVisitor(sys.argv[1]) visitor.run(startDir=sys.argv[2]) matches = visitor.matches print('Found', len(matches), 'files:') for fname in matches: print(fname)

""" Change all "#!...python" source lines at the top of Unix scripts to new line in all files in all dirs at and below a root; command line args are the root (default='.'), new #! line text (default=changeToDefault), and any text to run list-only mode (default=no);

could skip binary filename extensions explicitly, but try handler works; changes all #! first lines that name python, which is more accurate than a simple visitor_replace.py; example usage -- convert all scripts in book examples tree, list file to be changed in a tree, convert all to default: C:...\PP4E>python Tools\visitor_poundbang.py . #!\MyPy31\python > out.txt C:...\PP4E\Tools>python visitor_poundbang.py C:\temp\PP3E - - | more C:...\PP4E\Tools>python visitor_poundbang.py C:\temp\PP3E """

import sys from visitor import FileVisitor # reuse the walker classes changeToDefault = '#!\Python31\python.exe' # used if no cmdline arg

class PoundBangFixer(FileVisitor): def init(self, changeTo=changeToDefault, listonly=False, trace=0): FileVisitor.init(self, trace=trace) self.changeTo = changeTo self.listOnly = listonly self.clist = []

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
def visitfile(self, fullname):
    FileVisitor.visitfile(self, fullname)
    try:
        lines = open(fullname, 'r').readlines()      # fails for binary files
    except UnicodeDecodeError:
        if self.trace > 0: print('Skipped non-text file:', fullname)
    else:
        if (len(lines) > 0            and
            lines[0].startswith('#!') and            # or lines[0][0:2] == '#!'
            'python' in lines[0]                     # or lines[0].find() != -1
            ):
            self.clist.append(fullname)
            if not self.listOnly:
                lines[0] = self.changeTo + '\n'
                open(fullname, 'w').writelines(lines)

if name == 'main': if input('Are you sure?') != 'y': sys.exit() rootdir = sys.argv[1] if len(sys.argv) > 1 else '.' changeto = sys.argv[2] if len(sys.argv) > 2 else changeToDefault listonly = len(sys.argv) > 3 walker = PoundBangFixer(changeto, listonly) walker.run(rootdir) print('Visited %d files and %d dirs,' % (walker.fcount, walker.dcount), end=' ') print('found' if listonly else 'changed', len(walker.clist), 'files') for fname in walker.clist: print(fname)

删除

"Remove all .pyc bytecode files in a tree (visitor version)"

import sys, os from visitor import FileVisitor

class CleanPyc(FileVisitor): def init(self, trace=0): FileVisitor.init(self, context=0, trace=trace)

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
def visitfile(self, filepath):
    FileVisitor.visitfile(self, filepath)
    if filepath.endswith('.pyc'):
        print(filepath) 
        #os.remove(filepath)
        self.context += 1

if name == 'main': walker = CleanPyc() walker.run(sys.argv[1] if len(sys.argv) > 1 else '.') print('Visited %d files and %d dirs' % (walker.fcount, walker.dcount)) print('Removed %d files' % walker.context)


查找大文件

"find biggest/smallest .py files in a tree (visitor version)"

import sys, os, pprint from visitor import FileVisitor

class BigPy(FileVisitor): def init(self, trace=0): FileVisitor.init(self, context=[], trace=trace)

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
def visitfile(self, filepath):
    FileVisitor.visitfile(self, filepath)
    if filepath.endswith('.py'):
        print(filepath) 
        self.context.append((os.path.getsize(filepath), filepath))

if name == 'main': walker = BigPy() walker.run(sys.argv[1] if len(sys.argv) > 1 else '.') print('Visited %d files and %d dirs' % (walker.fcount, walker.dcount)) walker.context.sort() pprint.pprint(walker.context[:2]) pprint.pprint(walker.context[-2:])


visitor.py

""" #################################################################################### 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; #################################################################################### """

import os, sys

class FileVisitor: """ Visits all nondirectory files below startDir (default '.'); override visit* methods to provide custom file/dir handlers; context arg/attribute is optional subclass-specific state; trace switch: 0 is silent, 1 is directories, 2 adds files """ def init(self, context=None, trace=2): self.fcount = 0 self.dcount = 0 self.context = context self.trace = trace

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
def run(self, startDir=os.curdir, reset=True):
    if reset: self.reset()
    for (thisDir, dirsHere, filesHere) in os.walk(startDir):
        self.visitdir(thisDir)
        for fname in filesHere:                          # for non-dir files
            fpath = os.path.join(thisDir, fname)         # fnames have no path
            self.visitfile(fpath)

def reset(self):                                         # to reuse walker
    self.fcount = self.dcount = 0                        # for independent walks

def visitdir(self, dirpath):                             # called for each dir
    self.dcount += 1                                     # override or extend me
    if self.trace > 0: print(dirpath, '...')

def visitfile(self, filepath):                           # called for each file
    self.fcount += 1                                     # override or extend me
    if self.trace > 1: print(self.fcount, '=>', filepath)

class SearchVisitor(FileVisitor): """ Search files at and below startDir for a string; subclass: redefine visitmatch, extension lists, candidate as needed; subclasses can use testexts to specify file types to search (but can also redefine candidate to use mimetypes for text content: see ahead) """

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
skipexts = []
testexts = ['.txt', '.py', '.pyw', '.html', '.c', '.h']  # search these exts

skipexts = ['.gif', '.jpg', '.pyc', '.o', '.a', '.exe'] # or skip these exts

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
def __init__(self, searchkey, trace=2):
    FileVisitor.__init__(self, searchkey, trace)
    self.scount = 0

def reset(self):                                         # on independent walks
    self.scount = 0

def candidate(self, fname):                              # redef for mimetypes
    ext = os.path.splitext(fname)[1]
    if self.testexts:
        return ext in self.testexts                      # in test list
    else:                                                # or not in skip list
        return ext not in self.skipexts
     
def visitfile(self, fname):                              # test for a match
    FileVisitor.visitfile(self, fname)
    if not self.candidate(fname):
        if self.trace > 0: print('Skipping', fname)
    else:
        text = open(fname).read()                        # 'rb' if undecodable
        if self.context in text:                         # or text.find() != -1
            self.visitmatch(fname, text)
            self.scount += 1

def visitmatch(self, fname, text):                       # process a match
    print('%s has %s' % (fname, self.context))           # override me lower

if name == 'main': # self-test logic dolist = 1 dosearch = 2 # 3=do list and search donext = 4 # when next test added

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
def selftest(testmask):
    if testmask & dolist:
       visitor = FileVisitor(trace=2)
       visitor.run(sys.argv[2])
       print('Visited %d files and %d dirs' % (visitor.fcount, visitor.dcount))

    if testmask & dosearch:
       visitor = SearchVisitor(sys.argv[3], trace=0)
       visitor.run(sys.argv[2])
       print('Found in %d files, visited %d' % (visitor.scount, visitor.fcount))

selftest(int(sys.argv[1]))    # e.g., 3 = dolist | dosearch
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
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 遍历目录类 脚本
""" #################################################################################### 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
7110
python 目录复制类 脚本
""" Use: "python ...\Tools\visitor_cpall.py fromDir toDir trace?" Like System\Filetools\cpall.py,
用户5760343
2022/05/13
5630
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
3850
python ftp工具类 上传、下载文件夹 封装类
-------------------------常用工具------------------------------------------------------------
用户5760343
2022/05/13
1.3K0
python 查找文件脚本 find.py
""" ################################################################################ Return all files matching a filename pattern at and below a root directory;
用户5760343
2022/05/13
4730
pkg文件--一种简单的游戏资源打包格式
[四字节] 固定的内容, 值不重要  [四字节] 文件数目(unsigned int)  [四字节] 文件名表 的偏移(unsigned int)  [四字节] 文件名表 的长度(字节数)(unsigned int)  ……  中间一堆 各个文件的内容, 文件内容使用zlib压缩过  ……  直到  文件名表:  [两字节] 文件名长度  [文件名长度那么多字节] 文件名  [四字节] 固定的内容,值不重要  [四字节] 文件原长度  [四字节] 文件偏移  [四字节] 文件压缩后的长度  [两字节] 又一个文件名的长度  …
用户7886150
2020/11/23
2.3K0
python开发_thread_线程_搜索本地文件
========================================================
Hongten
2018/09/13
5800
python开发_thread_线程_搜索本地文件
python 回归测试脚本
""" ################################################################################ Test a directory of Python scripts, passing command-line arguments, piping in stdin, and capturing stdout, stderr, and exit status to detect failures and regressions from prior run outputs. The subprocess module spawns and controls streams (much like os.popen3 in Python 2.X), and is cross-platform. Streams are always binary bytes in subprocess. Test inputs, args, outputs, and errors map to files in subdirectories.
用户5760343
2022/05/13
8430
python 回归测试脚本
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 目录复制 脚本
""" ################################################################################ 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
8700
python小工具
http://blog.csdn.net/pipisorry/article/details/46754515
用户7886150
2020/12/30
5910
树莓派3B+ 人脸识别(OpenCV)
首先,所有的方法都有类似的过积,即都使用了分好类的训练数据集(人脸数据库,每 个人都有很多样本)来进行“训练”,对图像或视频中检测到的人脸进行分析,并从两方面来确定:是否识别到目标,目标真正被识别到的置信度的度量,这也称为置信度评分。
全栈程序员站长
2022/09/12
9760
树莓派3B+ 人脸识别(OpenCV)
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
#6 ipdb模块源代码解读
前言 好久不见,大家最近可好?。通过前几节的学习,相信你已经掌握了面向对象的大量知识,但是光知道是不够的,需要自己多写、多看,学一门语言无非不过这两种秘诀嘛。因此本篇博文带着大家剖析一次源代码,剖析对
py3study
2020/01/17
9810
python代码实现linux下的tail功能
今天在服务器上调试程序,发现win03的服务器在查看apache输出日志的时候灰常麻烦,想到linux下系统的命令 tail就可以实时查看输出日志,于是找了下,还真有人写了个win下的tail:http://www.kuaipan.cn/file/id_12834302878348914.htm
the5fire
2019/02/28
1.9K0
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
9280
Python与DBA
 传统上,当需要为操作系统编写一些脚本时,人们常常会选用 Bash 或 Perl 脚本工具。这些工具易于使用,因而它们几乎变得无处不在,***到了包括 Oracle Database 在内的其他软件中,Oracle Database 在很大程度上依赖它们执行各种管理任务。
py3study
2020/01/09
1.2K0
python 跨平台python脚本启动器
------------------------------------调用方式-------------------------------- import Launcher Launcher.launchBookExamples(['PyDemos.pyw'], trace=False) ----------------------------------Launcher.py--------------------------------
用户5760343
2022/05/13
7230
Python:Pyqt5相关 安装-应用-简单使用
从开始有想法学习python到现在也大概半年了,断断续续的一直再看,主要是:廖雪峰Python链接如下:
何其不顾四月天
2023/03/10
5600
相关推荐
python 代码总行数统计 脚本
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档