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