Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Opens all .txt files in a folder and
- # searches for any line that matches
- # a user-supplied regular expression
- import re, os
- textFiles = os.listdir('C:\\Users\\Melina\\AppData\\Local\\Programs\\Python\\Python36-32\\Projects\\Automate the Boring Stuff')
- print('What do you want to search for?')
- userReg = str(input())
- if userReg is 'email':
- stringRegex = re.compile(r'[a-z0-9]+((\.|\_)[a-z0-9]+)*@[a-z0-9]+(\.[a-z0-9]+)*(\.[a-z0-9]{2,20})')
- #fileRegex = re.compile(r'\w+\.txt')
- elif userReg is 'phone':
- stringRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
- #fileRegex = re.compile(r'\w+\.txt')
- fileRegex = re.compile(r'\w+\.txt')
- for i in range(len(textFiles)):
- if fileRegex.search(textFiles[i]):
- openFile = open('C:\\Users\\Melina\\AppData\\Local\\Programs\\Python\\Python36-32\\Projects\\Automate the Boring Stuff\\' +textFiles[i])
- readFile = openFile.readlines()
- for line in range(len(readFile)):
- r = 0
- if stringRegex.search(readFile[r]):
- print(readFile[r])
- r = r+1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement