Python Development Search File script

Source: Internet
Author: User

#!/usr/bin/env python#coding:utf-8import OS, sys# determine positional parameters argv_num = Len (sys.argv) if argv_num! = 3:print ("Please enter at least 2-bit parameter:") pri NT ("########################################") print ("Example:python%s \033[1;31mfilename file_path\033[0m"% Sys.argv[0]) print ("########################################") exit () def search (Root_dir, Searc): try: # Gets the entire list directory and filename for the search path for (Abs_path, dirs, files) in Os.walk (root_dir): for filename in files:if searc in filename: #搜索文件名的索引 Position variable Searc_changdu = len (searc) Find_qian_num = Filename.find (searc) find_zongchang_num = Searc_changdu + find_qian_num #列 Table Shard Front Segment Highlight variable qian_1 = Filename[:searc_changdu] Hou_1 = Filename[searc_changdu:] #列表分片中段高亮显示变量 qian_2 = Filename[:find_ Qian_num] hou_2 = filename[find_qian_num:find_zongchang_num] #列表分片后段高亮显示变量 zhong = filename[find_qian_num:find_ Zongchang_num] Hou_3 = filename[find_zongchang_num:] #绝对路径及文件名高亮输出 if find_qian_num = = 0:print ("%s/\033[1;31m%s\033[0m %s "% (abs_path,qian_1,hou_1)) elif Find_zongchang_num = = Len (filenaMe): Print ("%s/%s\033[1;31m%s\033[0m"% (abs_path,qian_2,hou_2)) else:print ("%s/%s\033[1;31m%s\033[0m%s"% (Abs_path , qian_2,zhong,hou_3)) #用户中断执行 (usually input ^c) except Keyboardinterrupt, E:print ("Break Execution") search (sys.argv[2], sys.argv[1])

Execution effect Display

Python Development Search File script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.