仅打开和读取目​​录内的目录文件

时间:2018-12-07 22:54:21

标签: python regex python-3.x glob os.path

这是我的新代码。由于某种原因,它给出了下面提供的错误。有人知道为什么会这样吗?或者我可以使用任何方法来解决此问题?

新代码:

import glob
import re

folder_path = "/home/"
file_pattern = "/**/*"
folder_contents = glob.glob(folder_path + file_pattern, recursive=True)

#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:
    read_file = open(file, 'rt').read()
    if regex1.findall(read_file) or regex2.findall(read_file):

        email = regex1.findall(read_file)
        phone=regex2.findall(read_file)

        match_list.append(file)
        print (file)
        print (email)

以下是我收到的错误:

/home//sample.txt
['bcbs@aol.com', 'James@aol.com']
---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-44-6281ab1fc0ff> in <module>()
     15 
     16 for file in folder_contents:
---> 17     read_file = open(file, 'rt').read()
     18     if regex1.findall(read_file) or regex2.findall(read_file):
     19 

/jupyterhub_env/lib/python3.5/codecs.py in decode(self, input, final)
    319         # decode input (taking the buffer into account)
    320         data = self.buffer + input
--> 321         (result, consumed) = self._buffer_decode(data, self.errors, final)
    322         # keep undecoded input until the next call
    323         self.buffer = data[consumed:]

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc7 in position 10: invalid continuation byte

我是否需要添加if else语句来指定文件类型或................................ ................................................... ................................................... ................................................... ................................................... ......

2 个答案:

答案 0 :(得分:0)

glob模块通过指定recursive=True来允许此操作:

folder_path = "/home/e136320"
file_pattern = "/**/*"
folder_contents = glob.glob(folder_path + file_pattern, recursive=True)

答案 1 :(得分:0)

您的文件显然不在Python正在检测的语言环境编码中。您的语言环境正在寻找UTF-8数据,但看起来该文件采用其他某种编码。假设您主要在英语语言环境中工作,那么可能会有两个不错的猜测:cp1252latin-1;尝试将encoding='cp1252'传递给open调用,看看是否有效。 latin-1永远不会失败,但是它可能会产生乱码,而Windows机器经常会产生cp1252数据,这是一个很好的猜测。