Python(3):檔案讀寫與異常

檳城碼農發表於2016-08-15

訪問路徑:

檔案讀寫必然涉及到檔案會放在某個路徑下。在python裡,可以通過引入os包來實現切換當前訪問的路徑:

 1 # 假設我在 /home/zyq/KiDe/Python/test 資料夾中有一個檔案 test.txt 那麼我可以通過以下命    令定位到該資料夾:
 2 >>>import os
 3 >>>os.chdir(`/home/zyq/KiDe/Python/test`)
 4 # 此時可以通過 os.getcwd() 來得到當前的工作目錄。 
 5 # 此時可以通過如果下命令來進行檔案讀取測試:
 6 >>>data = open(`test.txt`)        
 7 >>>print(data.readline(), end=``)
 8 This is line1
 9 >>>print(data.readline(), end=``)
10 This is line2
11 >>>data.seek(0)                        # 此時將遊標又放到了檔案最開始
12 >>> for each_line in data:    
13         print(each_line, end=``)
14 >>>data.close()                        # 一定要記得關閉讀取流

DirRead

 

讀寫例項:

這裡我們來處理另一個類似的檔案。只不過這個檔案可以看做是兩個人對話的檔案,檔案內容如下:

 1 Man: You didn`t!
 2 Other Man:I`m telling you, I did!
 3 Man: You did not!
 4 Other Man:Oh I`am sorry, is this a five minute argument,or the full of the half hour?
 5 Man:Ah!(taking out his wallet and paying) Just the five minutes.
 6 Other Man:Just the five minutes. Thank you.
 7 Other Man:Anyway, I did.
 8 Man:You most certainly did not!
 9 Other Man:Now let`s get one thing quite clear:I most definitely old you!
10 Pause
11 Man:Oh no you didn`t!
12 Other Man: Oh yes I did!
13 End

Talk.txt

這個檔案假設為test2.txt,假設我們需要處理每一行的資料,將每個人的對話區別開來,我們可以使用python內建的split函式, 針對每一行資料採用如下:
(role, line_spoken) = each_line.split(“:”)。假設我們以此採用該程式碼來處理每一行,會發現報了一個:  ValueError:to many values to upack (expected 2) 。這是由於 Other Man:Now let`s get one thing quite clear:I most definitely old you! 這一句出現了兩個 “:” 。
 
我們可以通過 help(split)來檢視split函式說明,看先是否有可以選擇的引數來實現我們所需。

可以看到有一個 maxsplit引數來指定,對於間隔符的處理次數,我們這裡只要得到兩部分,即只要處理一次即可,所以可以修改每行讀取函式為:

1 >>>for each_line in data:
2 >>>    (role, line_spoke) = each_line.split(":", 1)
3 >>>    print(role, end=``)
4 >>>    print(" says: ", end=``)
5 >>>    print(line_spoke, end=``) 

NewSplit

這樣發現還是有問題,因為 Pause行 和 End行 根本沒法進行分割,這時有兩種解決方案:

 1 # 方案一: 使用 each_line.find(":")來判斷當前行中是否有分割符
 2 >>>for each_line in data:
 3 >>>    if not each_line.find(":") == -1:
 4 >>>        print(role, end=``)
 5 >>>           print(" says: ", end=``)
 6 >>>        print(line_spoke, end=``)
 7 # 這個方案不強壯且可能需要經常變動。此時可以用到異常:
 8 # 方案二: 使用try except來處理,對於異常處理如果使用pass的話表示忽略這個異常繼續執行:
 9 >>>for each_line in data:
10 >>>    try:
11 >>>        print(role, end=``)
12 >>>           print(" says: ", end=``)
13 >>>        print(line_spoke, end=``)        
14 >>>    except:
15 >>>        pass 

Solutions

可以發現異常只需簡單的處理就完成了想要的功能。接下來我們對異常進行具體化,假設要開啟的檔案不存在,在python中會丟擲 IOError 。當然我們也可以通過 if os.path.exists(test3.txt) 來判斷檔案是否存在。
這裡我們改進下異常處理程式碼,通過具化的異常捕獲來實現:

 1 try:
 2     data = open(`test3.txt`)
 3     for each_line in data:
 4         (role, line_spoken) = each_line.split(":", 1)
 5         print(role, end=``)
 6         print(" says: ", end=``)
 7         print(line_spoken, end=``)
 8 except ValueError:
 9     pass           # 表示忽略該異常,程式繼續往下執行
10 except IOError:
11     print("File not exists!")
12 finally:
13     if `data` in locals():                # 判斷data變數是否存在當前區域內
14         print("Close file!")
15         data.close() 

SpecificExcept

 

儲存資料到檔案:
去掉字串兩端的空格,python有一個 strip函式,使用方式和java的trim一樣。
python將資料寫入檔案大概包括下面三步:
1. 利用open函式開啟檔案進行寫,open預設的開啟方式是 “r”,表示檔案是用來開啟讀的,要用來寫可以使用 out = open(“test2.txt”, “w”) 。
2. 利用print函式的file引數,該引數預設是輸出到標準控制檯(sys.stdout ,可以引入sys來使用該變數),其實也可以指定檔案。使用方式如下:
print(“Hello”, file = out)。
3. 一定要記得關閉檔案輸入流。
PS: 開啟檔案進行寫的時候,如果檔案不存在會自動建立,使用w模式會清空檔案中的所有內容。如果想在原來檔案內容上追加,可以使用a模式(如果又想讀使用a+)。要開啟一個檔案用來讀和寫需要清除原來內容的話,使用w+。
修改前面的分別提取兩人的程式碼,將兩個人的說話內容分別寫入不同的檔案,總的程式碼如下:

 1 import os
 2 os.chdir(`/home/zyq/KiDe/Python/test`)
 3 man = []
 4 otherMan = []
 5 try:
 6     data = open(`test2.txt`)
 7     for each_line in data:
 8         (role, line_spoken) = each_line.split(":", 1)
 9         #print(role, end=``)
10         # Store the message to array by the man
11         line_spoken = line_spoken.strip()
12         if role == `Man`:
13             man.append(line_spoken)
14         else:
15             otherMan.append(line_spoken)  
16         #print(" says: ", end=``)
17         #print(line_spoken, end=``)
18 except ValueError:
19     pass
20 except IOError:
21     print("File not exists!")
22 finally:                            # 必定會執行的
23     if `data` in locals():      # 判斷data變數是否存在,即是否開啟了檔案流
24         print("Close file test2.txt!")
25         data.close()
26  
27 #write two arrays to file
28 try:
29     out1 = open("man.txt", "w")
30     out2 = open("otherMan.txt", "w")
31     
32     print(man, file = out1)
33     print(otherMan, file = out2)
34 
35 except IOError:
36     print("File write error!")
37 finally:
38     out1.close()
39     print("Close file man.txt!")
40     out2.close()
41     print("Close file otherMan.txt!")  

Write

PS:在python中的字串和java中一樣也是不可變的。
上面這個程式還有以下改進點:
1. 具體的異常資訊沒有被輸出, 這裡可以對except塊進行改進,改進後方式大致如下:
except IOError as err:
    print(“File error:” + str(err))        # 一定要是用str函式將異常轉換為字串,否則會丟擲型別不相容錯誤
2. 對於上面開啟的檔案,都需要在finally塊中將其關閉,其實python實現了jdk1.7之後才有的由編譯器自動關閉的方式,通過with方式可以簡單的實現該功能:
with open(`test.txt`, “w”) as data
另外with支援同時開啟多個檔案,只需要用逗號隔開即可:
 with open(`test.txt`, “w”) as data, open(`test2.txt`, “w”) as data2
基於以上兩點改進之後,新的程式碼如下:

 1 import os
 2 os.chdir(`/home/zyq/KiDe/Python/test`)
 3 man = []
 4 otherMan = []
 5 try:
 6     with open(`test2.txt`) as data:           # with開啟的不需要顯示的關閉
 7         for each_line in data:
 8             (role, line_spoken) = each_line.split(":", 1)
 9             # Store the message to array by the man
10             line_spoken = line_spoken.strip()
11             if role == `Man`:
12                 man.append(line_spoken)
13             else:
14                 otherMan.append(line_spoken)  
15 except ValueError:
16     pass
17 except IOError as err:
18     print("File read error:" + str(err))        # 輸出具體異常資訊
19  
20 #write two arrays to file
21 try:
22     with open("man.txt", "w") as out1, open("otherMan.txt", "w") as out2:
23         print(man, file = out1)
24         print(otherMan, file = out2)
25 except IOError as err:
26     print("File write error:" + str(err)) 

AutoClose

假設我現在想從檔案中讀出資料,直接使用readline可以發現檔案中的所有內容都會被一股腦的輸出出來,當然這時我們可以改進前面的print_list函式,使其增加一個輸出到的位置引數 。使當時寫入檔案的列表資料按行來拆分。但是這樣會導致函式的引數越來越多,使用更加複雜。所以這裡我們使用另外一種方式: 引入pickle(醃製)包,並使用dump來寫入資料,load來讀出資料。此時需要注意寫入和讀出資料的時候模式應該用 “wb” 和 “rb”,表示使用二進位制的方式。使用pickle的時候可能丟擲異常為PickleError,最終我們將程式碼改為:

 1 import os
 2 import pickle
 3 os.chdir(`/home/zyq/KiDe/Python/test`)
 4 man = []
 5 otherMan = []
 6 try:
 7     with open(`test2.txt`) as data:
 8         for each_line in data:
 9             (role, line_spoken) = each_line.split(":", 1)
10             # Store the message to array by the man
11             line_spoken = line_spoken.strip()
12             if role == `Man`:
13                 man.append(line_spoken)
14             else:
15                 otherMan.append(line_spoken)  
16 except ValueError:
17     pass
18 except IOError as err:
19     print("File read error:" + str(err))
20  
21 #write two arrays to file
22 try:
23     with open("man.txt", "wb") as out1, open("otherMan.txt", "wb") as out2:
24         pickle.dump(man, file = out1)
25         pickle.dump(otherMan, file = out2)
26 except IOError as err:
27     print("File write error:" + str(err))
28  
29 # 此時如果想讀取man.txt檔案中的資訊的話,只需使用:
30 import pickle
31 with open(`man.txt`, `rb`) as data:
32     list = pickle.load(data)
33 # 此時list就是原來的列表。可以直接通過list[0]、list[1] 等來訪問列表中的對應項。

Pickle

 

黎明前最黑暗,成功前最絕望!


相關文章