आप सभी फ़ाइलों और निर्देशिकाओं पर जाने के लिए os.walk का उपयोग कर सकते हैं। फिर आप फ़ाइल नामों में मिलान करने वाले सरल पैटर्न (जैसे कि आपने प्रश्न में दिए गए उदाहरण) कर सकते हैं।
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
यदि आप चाहें तो इसे बाद में उपयोग करने के लिए किसी सूची में पथ जोड़ सकते हैं। आप पूरी फ़ाइल नाम का उपयोग करना चाहते हैं तो आप os.path.join
os.path.join(path, name)
उपयोग कर सकते हैं आप एक फ़ाइल के भीतर पैटर्न उपयोग करना चाहते हैं, तो आप के रूप में नीचे दिए गए कोड को संशोधित कर सकते हैं।
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
स्रोत
2017-07-27 07:18:18
उत्तर के लिए धन्यवाद –
आपका स्वागत है! मदद करने के लिए खुश :) –