亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Py 正則表達式與 os 庫

Py 正則表達式與 os 庫

不負相思意 2023-02-15 15:30:53
我正在嘗試根據下面的代碼查找正則表達式。問題似乎與 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')

為了加入文件的名稱以及文件的路徑。


查看完整回答
反對 回復 2023-02-15
  • 1 回答
  • 0 關注
  • 126 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號