我正在嘗試根據(jù)下面的代碼查找正則表達(dá)式。問題似乎與 open() 函數(shù)周圍的文件路徑有關(guān),盡管彈出的錯(cuò)誤顯示了適當(dāng)?shù)哪夸?。試圖自己尋找解決方案,但似乎我的初學(xué)者技能還不夠,老實(shí)說,就像 2 周前開始學(xué)習(xí) 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()錯(cuò)誤如下: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貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊
改變:
for f_ in file: myfile = open(f_,'r')
到:
for f_ in file: myfile = open(os.join(root,f_),'r')
為了加入文件的名稱以及文件的路徑。
添加回答
舉報(bào)
0/150
提交
取消