Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! Python3
- # This uses a regex to search multiple text files for a certain string
- import os, re
- print('The current working directory is', os.getcwd())
- print('''\nWe want to examine files in the TestText folder which is held in the
- current directory. In order to do so, we need to change directory''')
- os.chdir('.\\TestText')
- changeDir = str(os.getcwd())
- print('\n', changeDir)
- print('\nThe current path is now', os.getcwd(), '\n')
- print('And now we want to see what files exist within that directory')
- fileList = os.listdir('.')
- print(fileList)
- print('\nNow we need to create a regex to search our files...')
- textSearch = re.compile(r'two', re.I)
- #create a loop to go through the list
- for i in fileList:
- textFile = open(changeDir + '\\' + i)
- textFile.read()
- contents = textFile.read()
- print(contents)
- textFile.close()
- textSearch.findall(contents)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement