python - 过滤目录中的所有文件以查找与多个正则表达式匹配的单词

标签 python regex glob pypdf os.path

我正在尝试过滤目录中与我的正则表达式匹配的单词的所有文件(pdf、txt、csv、ipynp 等)。到目前为止,我制作了一个可以读取 csv 和 pdf 文件的程序(如下所示),但读取所有其他文件类型的 else 语句一直给我一个错误(如底部所示)。我在 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] is a directory: 你知道为什么每当我运行代码时都会显示此错误消息吗?

  ---------------------------------------------------------------------------
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

关于python - 过滤目录中的所有文件以查找与多个正则表达式匹配的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53820407/

相关文章:

regex - Sublime Text :正则表达式删除不以开头的行

bash - 在 Bash 中通配重音文件

python - sqlalchemy postgres : AttributeError: can't set attribute

python - 使用 Python 从 3D 中的六个点确定齐次仿射变换矩阵

.net - 正则表达式头痛

java - Xpath 编译正则表达式不显示 xml 注释

python - glob.glob 的意外输出

php - 如何在 php glob() 中定义多个模式

python - 带有逻辑运算符的乌龟 ORM 过滤器

python - 减去下一行使用当前行,python 数据框