[英]Read all files in directory and output the files that contain certain regexes in them
我正在嘗試讀取目錄中的所有文件,並輸出包含正則表達式的文件以及每個文件中的正則表達式。
import glob
import re
import PyPDF2
#-------------------------------------------------Input----------------------------------------------------------------------------------------------
folder_path = "/home/"
file_pattern = "/*"
folder_contents = glob.glob(folder_path + file_pattern)
#Search for Emails
regex1= re.compile(r'\S+@\S+')
#Search for Phone Numbers
regex2 = re.compile(r'\d\d\d[-]\d\d\d[-]\d\d\d\d')
match_list=[]
for file in folder_contents:
if re.search(r".*(?=pdf$)",file):
#this is pdf
with open(file, 'rb') as pdfFileObj:
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
pageObj = pdfReader.getPage(0)
content = pageObj.extractText()
read_file = open(file,'rb')
#print("{}".format(file))
elif re.search(r".*(?=csv$)",file):
#this is csv
with open(file,"r+",encoding="utf-8") as csv:
read_file = csv.read()
#print("{}".format(file))
elif re.search(r"/jupyter",file):
print("wow")
elif re.search(r"/scikit",file):
print("wow")
else:
read_file = open(file, 'rb').read()
#print("{}".format(file))
continue
if regex1.findall(read_file) or regex2.findall(read_file):
print(read_file)
我設法寫了下面的代碼,但它給出了以下錯誤:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-39-f614d35e0441> in <module>()
38 #print("{}".format(file))
39 continue
---> 40 if regex1.findall(read_file) or regex2.findall(read_file):
41 print(read_file)
TypeError: expected string or bytes-like object
有什么辦法可以使它正常工作而不會出錯?
以此替換您的讀取文件代碼:
with open(File, mode='rb') as file:
readFile = file.read()
使用read()
僅open(filename)
將起作用。 只需替換為這個,您就可以解決問題。
read_file = open(file).read()
首先,我向其他回答這個問題的人表示歉意,因為我會說一些關於OP以前的問題。
關於OP,您不應無意識地復制代碼。
Content
是您已經閱讀的頁面。 這意味着您的代碼應為read_file = content
。 以及為什么我編寫read_file = #
,因為我認為您將添加額外的代碼。 但它不應再次讀取同一文件。
with open(file, 'rb') as pdfFileObj:
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
pageObj = pdfReader.getPage(0)
content = pageObj.extractText()
read_file = open(file,'rb')
#^---^---^ according to your former question, `read_file` should be `content`
而且還會出現其他問題。 您應該在print("wow")
之后添加continue
。
elif re.search(r"/jupyter",file):
print("wow")
elif re.search(r"/scikit",file):
print("wow")
否則您的代碼將繼續運行,然后發生錯誤。 因為你什么都沒讀。
if regex1.findall(read_file) or regex2.findall(read_file):
print(read_file)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.