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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With