Python folder traversal and file lookup __python

Source: Internet
Author: User
Tags parent directory throw exception
#-*-Coding:utf-8-*-#to find where to use the "table on xxxxx xxxxxx production env '" In the project our metadata management is not very good, if you know where a table has been used before, 
You need to write a program to traverse the "Import os import os.path RootDir =" C:\\users\\ibm_admin\\ibm\\rationalsdp\\workspace "# to indicate the folder being traversed query = "XXXXXXXXX" def Walk_all_files (rootdir,query): for parent,dirnames,filenames in OS                         Walk (RootDir): #for循环自动完成递归枚举 #三个参数: return 1. Parent directory (current path) 2. All folder names (excluding paths) 3. All file names for dirname in Dirnames:
            #输出文件夹信息 #print "Parent is:" + parent #print "DirName is:" + dirname
            Pass to filename in filenames: #输出文件信息 #print ' parent is: ' + Parent
            #print ' filename is: ' + filename #print ' The full name of the ' file is: ' + os.path.join (parent,filename)  Is_file_contain_word (Os.path.join (parent,filename), query) def is_file_contain_word (File_,query_word): #print 1111111 if Query_woRd in Open (File_). Read (): print File_ filecontext = open (File_). Read () lines = Filecontext.split (  ' \ n ') # python print keywords in row for line in Lines:if Query_word in Line:print  Line Walk_all_files (rootdir,query) print "Do" http://www.iplaypy.com/jichu/note.html please explain Os.walk () : The Walk () method syntax format is as follows: Os.walk (top[, topdown=true[, onerror=none[, Followlinks=false]]) os.walk (top[, topdown=true[,  onerror=none[, Followlinks=false]]) parameter top-each folder (including itself) under the root directory, resulting in a 3-tuple (Dirpath, Dirnames, filenames) folder path, folder name,
    File name ". Topdown-optional, true or unspecified, the 3-tuple of a directory will be generated first (from top to bottom) in the 3-tuple of any of its subfolders.
    If Topdown is False, a 3-tuple of a directory will be generated after the 3-tuple of any of its subfolders (the directory is bottom-up). OnError--optional, is a function; It is called with a parameter, a OSError instance.
    After reporting this error, continue walk, or throw exception terminate walk.

Followlinks--set to True, the directory is accessed through a soft link.

Return value the method has no return value. '''

Related Article

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.