我正在尝试过滤我的目录中的所有文件(pdf,txt,csv,ipynp等),以查找与我的正则表达式匹配的单词。到目前为止,我制作了一个程序(如下所示),可以读取csv和pdf文件,但是else语句读取所有其他文件类型,一直给我一个错误(显示在底部)。我在其他地方输入了错误的声明吗?我尝试了一切但无济于事。
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')
#Search for Locations
regex3 =re.compile("([A-Z]\w+), ([A-Z]{2})")
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)
read_file = pageObj.extractText()
#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()
else:
with open(file,"rt", encoding='latin-1') as allOtherFiles:
continue
if regex1.findall(read_file) or regex2.findall(read_file) or regex3.findall(read_file):
print ("YES, This file containts PHI")
print(file)
else:
print("No, This file DOES NOT contain PHI")
print(file)
我收到一个错误,说IsAdirectoryError:[Errno 21]是一个目录:你知道为什么每当我运行代码时这个错误信息都会一直显示。
---------------------------------------------------------------------------
IsADirectoryError Traceback (most recent call last)
<ipython-input-40-fdb88fbf61ab> in <module>()
29 read_file = csv.read()
30 else:
---> 31 with open(file,"rt", encoding='latin-1') as allOtherFiles:
32 continue
33 if regex1.findall(read_file) or regex2.findall(read_file) or regex3.findall(read_file):
IsADirectoryError: [Errno 21] Is a directory: '/home/jupyter_shared_notebooks'
你能尝试改变你的with open(file,"rt") as allOtherFiles:
声明吗?
with open(file,"rt", encoding='latin-1') as allOtherFiles:
再次运行代码,看看是否遇到了同样的错误。如果仍有错误,我们将不得不尝试其他编码格式。
编辑:解决您的下一个错误:
IsADirectoryError: [Errno 21] Is a directory: /home/e136320/jupyter_shared_notebooks
这是由文件夹中名为jupyter_shared_notebooks
的文件或文件夹引起的。
因为python不知道如何打开jupyter_shared_notebooks
,因为它没有文件扩展名格式。它抛出了这个错误。
要解决这个问题,您可以尝试
if '.' not in file:
continue
else:
with open(file,"rt", encoding='latin-1') as allOtherFiles:
#rest of your code here