summaryrefslogtreecommitdiff
path: root/Chap8PracRegFiles.py
diff options
context:
space:
mode:
Diffstat (limited to 'Chap8PracRegFiles.py')
-rw-r--r--Chap8PracRegFiles.py29
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
+
+