Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. #! Python3
  2. # This uses a regex to search multiple text files for a certain string
  3.  
  4. import os, re
  5.  
  6. print('The current working directory is', os.getcwd())
  7. print('''\nWe want to examine files in the TestText folder which is held in the
  8. current directory. In order to do so, we need to change directory''')
  9.  
  10. os.chdir('.\\TestText')
  11. changeDir = str(os.getcwd())
  12. print('\n', changeDir)
  13. print('\nThe current path is now', os.getcwd(), '\n')
  14. print('And now we want to see what files exist within that directory')
  15. fileList = os.listdir('.')
  16. print(fileList)
  17.  
  18.  
  19. print('\nNow we need to create a regex to search our files...')
  20. textSearch = re.compile(r'two', re.I)
  21.  
  22. #create a loop to go through the list
  23. for i in fileList:
  24.     textFile = open(changeDir + '\\' + i)
  25.     textFile.read()
  26.     contents = textFile.read()
  27.     print(contents)
  28.     textFile.close()
  29.     textSearch.findall(contents)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement