几个常用程序
来源:互联网 发布:兴业银行淘宝金卡年费 编辑:程序博客网 时间:2024/06/05 10:16
搜索当前目录下的文件
知道大概的文件名称,使用
findf FileName
findf.py
import argparse, re, osfrom os.path import joinparser = argparse.ArgumentParser()parser.add_argument('FILENAME', help='file name use regular expression')parser.add_argument('-e', metavar='EXCLUDE', default=None, help='exclude file name use regular expression')args = parser.parse_args()count = 0for root, dirs, files in os.walk(os.getcwd()): for name in files: if not re.search(args.FILENAME, name): continue if args.e and re.search(args.e, name): continue count += 1 print(join(root, name))if count <= 1: print('\t %d file found.' % (count))else: print('\t %d files found.' % (count))
对应在 windows 下可以使用这样的命令:
dir /b /s FileName
在 linux 下可以使用这样的命令:
find . -name FileName -print
在当前目录及子目录中搜索文件包含的字符串
比如,看看哪些 java 文件中包含了 "Hello" 字符串
finds .java Hello
finds.py
import os, re, argparsefrom os.path import joinparser = argparse.ArgumentParser()parser.add_argument('-c', metavar='CODING', default='utf-8', help='coding: gbk, utf-8')parser.add_argument('EXT', help='.java, .txt')parser.add_argument('REGEX', help='regular expression')args = parser.parse_args()for root, dirs, files in os.walk(os.getcwd()): for name in files: if name[-len(args.EXT):] != args.EXT: continue count = 0 inFile = False for line in open(join(root, name), encoding=args.c).readlines(): count += 1 if re.search(args.REGEX, line): print(count, '\t', line, end='') inFile = True if inFile: print(join(root,name))
对应在 windows 下的命令:
findstr /s /c:"Hello" *.java
在 linux 下:
find . -name '*.java' -exec grep 'Hello' \{} \; -print
查找一个类所在的 jar 包
有时候,需要使用一个类,但不知道它在哪个 jar 包中。
findj ClassName
findj.py
import os, re, sysfrom os.path import joinfrom zipfile import ZipFiledef find_in_all_jars(matchstr, path): for root, dirs, files in os.walk(path): for name in files: if name[-4:] == '.jar' and handle_jar(matchstr, join(root, name)): print(join(root, name))def handle_jar(matchstr, filename): isMatch = False with ZipFile(filename) as zipfile: for name in zipfile.namelist(): if re.search(matchstr, name): print('\t', name) isMatch = True return isMatchif len(sys.argv) != 2: print('''Usage: findj ClassName.class''') sys.exit()#find_in_all_jars(sys.argv[1], os.getcwd()) # 在当前目录及子目录中所有 jar 包中查find_in_all_jars(sys.argv[1], 'e:\\data\\repository')
对应的 windows 命令:
for /r %i in (*.jar) do @jar tf %i | find "FastDateFormat" && echo %ifor /r %i in (*.jar) do @jar tf %i | find "FastDateFormat" & echo %i
Linux 命令
find . -name '*.jar' -exec jar tvf \{} \| grep Hello \; -print
字数统计
统计当前目录下对应文件的字数,单词数,行数
pwc .java
pwc.py
import os, re, argparsefrom os.path import joinparser = argparse.ArgumentParser()parser.add_argument('-c', metavar='CODING', default='utf-8', help='coding: gbk, utf-8')parser.add_argument('-d', action='store_true', default=False, help='print detail')parser.add_argument('EXT', help='.java, .txt')args = parser.parse_args()nf = nl = nw = nc = 0for root, dirs, files in os.walk(os.getcwd()): for name in files: if name[-len(args.EXT):] != args.EXT: continue nf += 1 nnl = nnc = nnw = 0 try: for line in open(join(root, name), encoding=args.c).readlines(): nnl += 1 nnc += len(line) nnw += len(line.split()) except: print('Passing file error: ' + join(root, name)) nl += nnl nc += nnc nw += nnw if args.d: print("%12d%10d%8d %s" % (nnc, nnw, nnl, join(root, name)))print("%12d%10d%8d%10d Files" % (nc, nw, nl, nf))
- js 几个常用程序
- 几个常用程序
- 数值分析常用的几个小程序C++实现
- 程序开发 很常用且实用的几个辅助软件
- C# 时间函数(几个常用时间,程序运行计时,页面运行计时)
- C# 时间(几个常用时间,程序运行计时,页面运行计时)
- 几个常用的JS
- 几个常用函数
- 几个常用的函数
- 几个常用函数
- 几个常用搜索引擎代码
- 几个常用函数
- linux 常用几个命令
- 几个常用js 函数!
- 几个常用的单词
- 几个常用网站
- 几个常用SHELL命令
- JFrame常用几个函数
- 什么是敏捷开发
- IOS中Json解析的四种方法
- 【原创】JAVA 通过SSH访问Windows(或Linux)服务器
- 第七篇 使用8253计数器
- 用JSLint精炼提升JavaScript代码
- 几个常用程序
- log-simple 研究--PHP学习积累
- C#2.0新特性
- 面试基础知识整理
- 压缩感知(Compressed Sensing)
- IOS学习记录--Objective-C--2012-11-14
- mahout配置流程
- 远程调试 Java 程序
- 单元测试实践的主要问题与解决(1)