推 lajji: 後面加'r'再試一次 03/01 17:33
→ lajji: files=zipfile.ZipFile("python.zip", "r") 03/01 17:33
→ alubasteve: 我試過了,很抱歉沒用,如果r前後有""的話 03/01 17:42
→ alubasteve: 還是找不到python.zip,可是我已經在Lib裡頭放了 03/01 17:44
→ alubasteve: 如果r沒有"",則顯示r沒有被定義 03/01 17:44
→ djshen: 沒人跟你說問別人之前先把錯誤訊息貼上來嗎 03/01 18:37
推 lajji: 不然就是你zip檔放錯地方 請google絕對路徑 相對路徑 03/01 18:38
→ alubasteve: 給djshen,多謝提醒,我怕錯誤訊息太長,反而眼花 03/01 19:07
→ alubasteve: 給lajji,多謝你提醒,我改成在檔名前追加絕對路徑 03/01 19:08
→ alubasteve: 然後就成了,所以問題是出在我的相對路徑抓不到 03/01 19:10
→ alubasteve: files=zipfile.ZipFile("c:\python\python.zip")即可 03/01 19:14
→ gmccntzx1: 不用怕訊息太長讓其他人眼花,善用線上服務如:codepad 03/01 19:27
→ gmccntzx1: , pastebin, gist 都可以讓你一次把大量的訊息傳上來 03/01 19:28
→ gmccntzx1: 你不給錯誤訊息的話,其他人可能還要花時間猜你實際上 03/01 19:28
→ gmccntzx1: 遇到的問題是什麼,這樣反而浪費大家時間又沒效率,所 03/01 19:29
→ gmccntzx1: 以貼個訊息,何樂不為? 03/01 19:29
→ gmccntzx1: 而且,像是你碰到的這個問題,光是丟錯誤訊息最後一行 03/01 19:31
→ gmccntzx1: 提到的 Exception 內容就可以略知一二囉 03/01 19:31
→ alubasteve: Traceback (most recent call last): 03/01 20:42
→ alubasteve: File "<pyshell#3>", line 1, in <module> 03/01 20:48
→ alubasteve: files=zipfile.ZipFile("python.zip") 03/01 20:48
→ alubasteve: File "C:\Users\user\AppData\Local\Programs\ 03/01 20:49
→ alubasteve: \Python\Python37\lib\zipfile.py", line 1182, in __ 03/01 20:49
→ alubasteve: self.fp = io.open(file, filemode) 03/01 20:50
→ alubasteve: FileNotFoundError: [Errno 2] No such file 03/01 20:50
→ alubasteve: or directory: 'python.zip' 03/01 20:51
→ alubasteve: 抱歉我弄巧成拙,請各位過目我碰到的問題訊息 03/01 20:52
→ djshen: python command怎麼下的 03/01 21:08
→ alubasteve: import zipfile 03/01 21:44
→ alubasteve: files=zipfile.ZipFile("python.zip") 03/01 21:45
→ djshen: 喔我是指command line 03/01 22:41
→ alubasteve: 給djshen,抱歉我不知道python command跟command line 03/01 22:45
→ alubasteve: 的差異,可以請你告訴我嗎 03/01 22:51
→ djshen: 你是把code存在一個xx.py 然後用python xx.py執行? 03/01 23:09
→ gmccntzx1: 看他上面個訊息有 `pyshell#3` ,應該是在 cmd 中直接 03/01 23:48
→ gmccntzx1: 執行的 03/01 23:48
→ gmccntzx1: 依原文來看,原 PO 應該是把讀檔的概念弄錯。你要讀檔 03/01 23:55
→ gmccntzx1: 時,必須先知道要被讀取的檔案的路徑,之後在呼叫 open 03/01 23:56
→ gmccntzx1: 或其他讀檔的指令時(如你現在要用的 zipfile )再把該 03/01 23:57
→ gmccntzx1: 檔的路徑放進去,而不是純粹寫個檔案名稱就好。 03/01 23:57
→ gmccntzx1: 如果還是不太清楚我所說的,你可以試試看用 03/01 23:58
→ gmccntzx1: os.path.abspath() 去看你現在給的 'python.zip' 對於 03/01 23:59
→ gmccntzx1: 目前程式所讀到的路徑是什麼。 03/01 23:59
→ gmccntzx1: 舉個例,假設目前 os.getcwd() 的結果是 'C:\User\foo' 03/02 00:02
→ gmccntzx1: 那麼 os.path.abspath('python.zip') 所解析出的路徑就 03/02 00:02
→ gmccntzx1: 是 'C:\User\foo\python.zip' 03/02 00:02
→ gmccntzx1: 所以如果實際上你的 python.zip 不在 C:\User\foo 底下 03/02 00:04
→ gmccntzx1: 的話,那麼程式當然會讀不到檔案 03/02 00:04
→ gmccntzx1: 而你要做的事情應該是把正確的檔案路徑傳給 ZipFile , 03/02 00:06
→ gmccntzx1: 而非把檔案移到 '...\Python37\lib\' 底下 03/02 00:07
→ alubasteve: 給gmccntzx1,我就是這樣做,我改成 03/02 00:14
→ alubasteve: import zipfile 03/02 00:14
→ alubasteve: zip_file=zipfile.ZipFile("C:\python\python.zip") 03/02 00:15
→ alubasteve: 追加絕對路徑在檔名之前之後就不會再出現錯誤訊息 03/02 00:22
→ gmccntzx1: 收到,在你 19:14 的回應已經知道你解決了,我只是想 03/02 00:22
→ gmccntzx1: 告訴你不用因為怕錯誤訊息太多而不跟著文章附上來 03/02 00:23
→ alubasteve: 不會,很感謝你的熱心解說 03/02 00:27
→ alubasteve: 順便問一句,我試著全解壓縮python.zip,但是似乎只能 03/02 00:32
→ alubasteve: 解壓縮到原有路徑C:\python,請問這樣合理嗎 03/02 00:34
→ gmccntzx1: 參數 path 作為解壓縮的目的地 03/02 00:39
→ alubasteve: import zipfile 03/02 00:40
→ alubasteve: zip_file=zipfile.ZipFile("C:\python\python.zip") 03/02 00:43
→ alubasteve: zip_file.extractall("C:\python") 03/02 00:43
→ alubasteve: zip_file.close() 03/02 00:43
→ alubasteve: 上面沒問題 ,但是第三行改成 03/02 00:46
→ alubasteve: zip_file.extractall("C:\python\2") 就有問題 03/02 00:50
→ alubasteve: Traceback (most recent call last): 03/02 00:50
→ alubasteve: File "<pyshell#2>", line 1, in <module> 03/02 00:50
→ alubasteve: zip_file.extractall("C:\python\2") 03/02 00:51
→ alubasteve: File "C:\Users\user\AppData\Local\Programs\ 03/02 00:51
→ alubasteve: \Python\Python37\lib\zipfile.py", line 1594, 03/02 00:52
→ alubasteve: , in extractall 03/02 00:52
→ alubasteve: self._extract_member(zipinfo, path, pwd) 03/02 00:53
→ alubasteve: File "C:\Users\user\AppData\Local\Programs\ 03/02 00:53
→ alubasteve: \Python37\lib\zipfile.py", line 1640, in _extract_ 03/02 00:54
→ alubasteve: member 03/02 00:55
→ alubasteve: os.makedirs(upperdirs) 03/02 00:55
→ alubasteve: File "C:\Users\user\AppData\Local\Programs\ 03/02 00:56
→ alubasteve: Python\Python37\lib\os.py", line 221, 03/02 00:56
→ alubasteve: in makedirs 03/02 00:56
→ alubasteve: mkdir(name, mode) 03/02 00:57
→ alubasteve: OSError: [WinError 123] 檔案名稱、目錄名稱 03/02 00:57
→ alubasteve: 或磁碟區標籤語法錯誤。: 'C:\\python\x02' 03/02 00:57
→ gmccntzx1: 用一般字串表示路徑時, `\` 要改為 `\\` ,也就是說 03/02 01:02
→ gmccntzx1: 你要寫成 zip_file.extractall("C:\\python\\2") 03/02 01:03
→ gmccntzx1: 或著你可以改用 raw string,也就是改成 03/02 01:03
→ gmccntzx1: zip_file.extractall(r"C:\python\2") 03/02 01:03
→ gmccntzx1: 詳細差異可以 google python raw string 03/02 01:04
→ gmccntzx1: 另外,關於路徑的串接,可以再看看 os.path.join() 和 03/02 01:05
→ gmccntzx1: pathlib (for python 3) 03/02 01:06
→ gmccntzx1: 然後,你如果不想用 pastebin/codepad 貼文字的話,可 03/02 01:07
→ gmccntzx1: 以改成螢幕截圖後上傳到 imgur 再貼上來 03/02 01:08
→ gmccntzx1: 因為看你一行一行貼感覺很累... 03/02 01:09
→ alubasteve: 不好意思我是第一次聽說pastebin ,所以不大敢用 03/02 07:50
→ alubasteve: 剛才試用才知道還不算難 03/02 07:51
→ gmccntzx1: 上面說「看你一行一行貼感覺很累」是在說:你花好幾分 03/02 10:07
→ gmccntzx1: 鐘把訊息慢慢貼上來,不會累嗎? 所以才跟你說有什麼 03/02 10:07
→ gmccntzx1: 現有的工具可以讓你更方便啦 XD 03/02 10:07
→ gmccntzx1: 然後重點是,指解壓縮到另一個路徑的問題解決了嗎? 03/02 10:08
→ Raymond0710: 路徑的\ 改成\\ 03/02 10:19
→ Raymond0710: 讓別人了解你的問題很重要 辛苦推文大大 03/02 10:21
→ alubasteve: 感謝gmccntzx1開示,我兩種路徑的方法試過都OK了 03/02 13:27
→ alubasteve: 也試過直接解壓縮在新路徑資料夾上 03/02 13:29
→ alubasteve: 也謝謝前文諸位網友指教,讓我可以快速抓到盲點在哪 03/02 13:30
→ alubasteve: 我上午出門所以回覆的比較晚,請gmccntzx1多包涵 03/02 13:33
→ gmccntzx1: 不用在意,問題有解決就好! 03/02 17:24