带操作系统库的Py正则表达式

2024-05-23 13:43:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在根据下面的代码查找正则表达式。问题似乎出在open()函数的文件路径上,尽管弹出的错误显示在适当的目录下。我试图自己找到解决方案,但似乎我的初学者技能还不够,老实说,我是在两周前开始学习Python的。任何帮助都将不胜感激

def unzip_guide():
        src_path = file_path()
        pattern = '\\d{3}-\\d{3}-\\d{4}'
        for root, dir, file in os.walk(src_path):
            for f_ in file:
                if f_.endswith('zip'):
                    zippy = zipfile.ZipFile(src_path + '\\' + f_,'r')
                    zippy.extractall(src_path)
                    zippy.close()
        for root, dir, file in os.walk(src_path):
            for folder in dir:
                if folder.endswith('content'):
                    os.chdir(src_path + '\\' + folder)
                    cwd = os.getcwd()
                    for root,dir,file in os.walk(cwd):
                        for f_ in file:
                            myfile = open(f_,'r')
                            matches = []
                            reg = reg = re.compile('\\d{3}-\\d{3}-\\d{4}')
                            for line in myfile:
                                matches += reg.findall(line)
                            myfile.close()

错误如下:

File "C:/Users/XYZ/PycharmProjects/puzzle/puzzle.py", line 34, in unzip_guide
    myfile = open(cwd + '\\' + f_,'r')
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\XYZ\\Downloads\\Complete-Python-3-Bootcamp-master\\Complete-Python-3-Bootcamp-master\\12-Advanced Python Modules\\08-Advanced-Python-Module-Exercise\\extracted_content\\AEITMYIRQLP.txt'

Tags: pathinsrcforosdirrootopen