Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list folders/directories that contain particular pattern files in them in python?

For example, if file contains extension .parq I have to list all the directories present in the folder:

  • /a/b/c/
  • /a/b/e/
  • /a/b/f/

Here I have to list the directories c, e, f which has particular pattern files.

like image 313
Jaya Avatar asked Jul 27 '17 06:07

Jaya


Video Answer


1 Answers

You can use os.walk to traverse over all the files and directories. Then you can do simple pattern matching in the file names (like the example you gave in the question).

import os

for path, subdirs, files in os.walk('.'): #Traverse the current directory
    for name in files:
        if '.parq' in name:  #Check for pattern in the file name
            print path

You can either append the path to a list to use it later if you want. If you want to access the full file name you can use os.path.join

os.path.join(path, name)

If you want to access patterns within a file, you can modify the code as below.

import os

for path, subdirs, files in os.walk('.'):
    for name in files:
        with open(name) as f:
            #Process the file line by line  
            for line in f:       
                if 'parq' in line:
                    #If pattern is found in file print the path and file   
                    print 'Pattern found in directory %s' %path,
                    print 'in file %s' %name
                    break
like image 70
Gambit1614 Avatar answered Oct 20 '22 15:10

Gambit1614