我正在嘗試根據下面的代碼查找正則表達式。問題似乎與 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
提交
取消