Skip to content

Commit 03865c2

Browse files
authored
Update genericRoutines.py
1 parent eba01f5 commit 03865c2

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

genericRoutines.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,25 @@ def super_reduced_string(s):
159159
else:
160160
return 'Empty String'
161161
print(super_reduced_string('aaabccddd') )
162+
163+
164+
###########################################################################################################################
165+
#we want to write a program that takes a list of filenames as arguments and to print only the line which has a particular substring,
166+
like grep command in unix.
167+
def readfiles(filenames):
168+
for f in filenames:
169+
with open(f) as fr:
170+
for line in open(fr):
171+
yield line
172+
173+
def grep(pattern, lines):
174+
return (line for line in lines if pattern in line)
175+
176+
def printlines(lines):
177+
for line in lines:
178+
print(line)
179+
180+
def main(pattern, filenames):
181+
lines = readfiles(filenames)
182+
lines = grep(pattern, lines)
183+
printlines(lines)

0 commit comments

Comments
 (0)