-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chapter8RegexSearchProject.py
46 lines (36 loc) · 1.25 KB
/
Chapter8RegexSearchProject.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#! usr/bin/env python3
#Chapter 8 Practive Project - Regex Search
#This is a program that
#opens all .txt files in a folder and searches for any line that
#matches a user-supplied regular expression. The results will print to
#the screen
#Import dependencies
import os, re, pprint
#Ask for pathway for the folder where the .txt files are
print ('Please provide the folder pathway:')
pathway = input()
while os.path.isdir(pathway) == False:
print ('Please provide a correct folder pathway:')
pathway = input()
if os.path.isdir(pathway) == True:
print ('Pathway stored')
break
#Ask for the user-supplied regex
print('Please provide your regular expression:')
inputRegex = str(input())
#Create regex
userRegex = re.compile(inputRegex)
#Make list of files in the pathway
fileList = os.listdir(pathway)
#Open the files. Read the files. Put all the text into a string
folderContent = ''
for i in range(len(fileList)):
fileNext = open(pathway + '/' + fileList[i])
fileNextContent = fileNext.read()
folderContent = folderContent + fileNextContent
#Close file? (pathway + str(fileList[i])).close()
#Search string for regex
findAll = userRegex.findall(folderContent)
finalPrint = ' '.join(findAll)
#Print string
print (finalPrint)