我正在尝试根据下面的代码查找正则表达式。问题似乎与 open() 函数周围的文件路径有关,尽管弹出的错误显示了适当的目录。试图自己寻找解决方案,但似乎我的初学者技能还不够,老实说,就像 2 周前开始学习 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'
1 回答
FFIVE
TA贡献1797条经验 获得超6个赞
改变:
for f_ in file: myfile = open(f_,'r')
到:
for f_ in file: myfile = open(os.join(root,f_),'r')
为了加入文件的名称以及文件的路径。
添加回答
举报
0/150
提交
取消