summaryrefslogtreecommitdiff
path: root/Chap8PracRegFiles.py
blob: 254a324f86a66904700acb12196a8ad70caaaea1 (plain)
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
#! \usr\bin\env python3

# Chapter 8 Practice Regex Text Files

# Opens all .txt files in a folder and
# then searches for any line that matches
# a user-supplied regular expression.

import os
import re

dirfiles = os.listdir('C:\\gam')

print('What text do you want to search for?')
userReg = str(input())
stringRegex = re.compile(userReg)
fileRegex = re.compile(r'\w+\.txt')

for i in range(len(dirfiles)):
    if fileRegex.search(dirfiles[i]):
        openFile = open('C:\\gam\\' + dirfiles[i])
        readFile = openFile.readlines()
        for line in range(len(readFile)):
            r = 0
            if stringRegex.search(readFile[r]):
                print(readFile[r])
                r = r + 1