diff options
author | mlot <petri-rush-curvy@duck.com> | 2025-06-06 13:40:57 -0400 |
---|---|---|
committer | mlot <petri-rush-curvy@duck.com> | 2025-06-06 13:40:57 -0400 |
commit | 75a42ec54dbf721caa659ddf02c1f46fc2cb4bef (patch) | |
tree | 84be794a2481e356a7784557a6f9fb6fbf29cfdd /Chap8PracRegFiles.py |
Diffstat (limited to 'Chap8PracRegFiles.py')
-rw-r--r-- | Chap8PracRegFiles.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/Chap8PracRegFiles.py b/Chap8PracRegFiles.py new file mode 100644 index 0000000..254a324 --- /dev/null +++ b/Chap8PracRegFiles.py @@ -0,0 +1,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 + + |