比这篇新的文章: Codee#8161
比这篇旧的文章: Codee#8159

命令行小工具:输出系统Path中的符合条件的文件全路径,Python脚本

语言: Python, 标签: 命令行 2009/11/25发布 9个月前更新
作者: 半瓶墨水, 点击527次, 评论(0), 收藏者(0), , 打分:

背景
主题: 字体:
01 #What  : a small tool to locate files in system "PATH" variable
02
03 import sys
04
05 if len(sys.argv) < 2:
06    print "  Usage:"
07    print "    where.py test     #  normal  search"
08    print "    where.py te*      #  blurred search"
09    print "    where.py -x te*.cmd   #  regular expression search"
10    sys.exit()
11 elif len(sys.argv) == 2:
12    pattern = sys.argv[1].replace(".", "\\.").replace("*", "\\*").replace("?", ".?").lower()
13 else:
14    pattern = sys.argv[2]#tricky, no "-x" checking here
15
16 import os
17 paths = [p for p in os.getenv('PATH').split(";") if p]
18 exts = [ext.lower() for ext in os.getenv('PATHEXT').split(";") if ext]
19 paths.append(".\\")
20 import re
21 for p in paths:
22   if not os.path.isdir(p):
23     continue
24   for f in os.listdir(p):
25     if re.search(pattern, f.lower()):
26       is_exe = False
27       for ext in exts:
28         if f.endswith(ext):
29           is_exe = True
30           break
31       if is_exe:
32         print os.path.join(p,f)


所有评论,共0条:( 我也来说两句)


发表评论

注册登录后再发表评论