一文搞懂文件操作與異常模塊
本文轉(zhuǎn)載自微信公眾號(hào)「數(shù)據(jù)STUDIO」,作者云朵君 。轉(zhuǎn)載本文請(qǐng)聯(lián)系數(shù)據(jù)STUDIO公眾號(hào)。
大家好!我是云朵君,今天給大家?guī)硪黄狿ython文件操作與異常處理,這兩個(gè)部分往往是初學(xué)者入門時(shí)容易忽略的部分。大家重點(diǎn)的精力都放在如何寫出高大上的算法,如何畫出酷炫的圖形,如何使用機(jī)器學(xué)習(xí)模型等等,而常常容易忽略Python文件操作與異常處理,這兩個(gè)看似不起眼卻在python中卻擔(dān)著至關(guān)重要的角色。下面我們就來一起看看吧。
文件操作
我們的程序可以讀取文件,也可以寫入文件。默認(rèn)情況下,文件以讀模式('r')打開,但也可以以寫模式('w')和附加模式('a')打開。
你的程序可以從文件中讀取信息,也可以向文件中寫入數(shù)據(jù)。從文件中讀取可以讓你處理各種各樣的信息;寫入文件允許用戶在下次運(yùn)行你的程序時(shí)重新開始。您可以將文本寫入文件,還可以將Python結(jié)構(gòu)(如列表)存儲(chǔ)在數(shù)據(jù)文件中。
讀取文件
要從文件中讀取,程序需要打開文件,然后讀取文件的內(nèi)容。您可以一次讀取文件的全部?jī)?nèi)容,也可以逐行讀取文件。with語(yǔ)句確保當(dāng)程序完成對(duì)文件的訪問后,文件被正確地關(guān)閉。
- 一次讀取整個(gè)文件
- filename = 'siddhartha.txt'
- with open(filename) as f_obj:
- contents = f_obj.read()
- print(contents)
- 逐行讀取
從文件中讀取的每一行在行尾都有一個(gè)換行符,而print函數(shù)會(huì)添加它自己的換行符。rstrip()方法消除了打印到終端時(shí)會(huì)產(chǎn)生的額外空白行。
- filename = 'siddhartha.txt'
- with open(filename) as f_obj:
- for line in f_obj:
- print(line.rstrip())
- 將行存儲(chǔ)在列表中
- filename = 'siddhartha.txt'
- with open(filename) as f_obj:
- lines = f_obj.readlines()
- for line in lines:
- print(line.rstrip())
- 寫入文件
將'w'參數(shù)傳遞給open()告訴Python你想寫入文件。小心:如果文件已經(jīng)存在,這將刪除文件的內(nèi)容。
傳遞'a'參數(shù)告訴Python你想要添加到一個(gè)現(xiàn)有文件的末尾。
- 寫入一個(gè)空文件
- filename = 'programming.txt'
- with open(filename, 'w') as f:
- f.write("I love programming!")
- 向空文件寫入多行
- filename = 'programming.txt'
- with open(filename, 'w') as f:
- f.write("I love programming!\n")
- f.write("I love creating new games.\n")
- 追加寫入文件
- filename = 'programming.txt'
- with open(filename, 'a') as f:
- f.write("I also love working with data.\n")
- f.write("I love making apps as well.\n")
文件路徑
當(dāng)Python運(yùn)行open()函數(shù)時(shí),它會(huì)在存儲(chǔ)正在執(zhí)行的程序的同一目錄中查找文件。可以使用相對(duì)路徑從子文件夾中打開文件。也可以使用絕對(duì)路徑來打開系統(tǒng)中的任何文件。
- 從子文件夾中打開文件
- f_path = "text_files/alice.txt"
- with open(f_path) as f_obj:
- lines = f_obj.readlines()
- for line in lines:
- print(line.rstrip())
- 使用絕對(duì)路徑打開文件
- f_path = "/home/ehmatthes/books/alice.txt"
- with open(f_path) as f_obj:
- lines = f_obj.readlines()
- 在Windows上打開文件
Windows有時(shí)會(huì)錯(cuò)誤地解釋正斜杠。如果遇到這種情況,請(qǐng)?jiān)谖募窂街惺褂梅葱备堋?/p>
- f_path = r"C:\Users\ehmatthes\books\alice.txt"
- with open(f_path) as f_obj:
- lines = f_obj.readlines()
Except異常模塊
異常是幫助程序以適當(dāng)方式響應(yīng)錯(cuò)誤的特殊對(duì)象。例如,如果程序試圖打開一個(gè)不存在的文件,可以使用異常來顯示一個(gè)信息豐富的錯(cuò)誤消息,而不是使程序崩潰。
將可能導(dǎo)致錯(cuò)誤的代碼放置在try塊中。響應(yīng)錯(cuò)誤時(shí)應(yīng)該運(yùn)行的代碼位于except塊中。只有在try塊成功時(shí)才應(yīng)該運(yùn)行的代碼被放入else塊。
- prompt = "How many tickets do you need? "
- num_tickets = input(prompt)
- try:
- num_tickets = int(num_tickets)
- except ValueError:
- print("Please try again.")
- else:
- print("Your tickets are printing.")
try-except代碼塊
- 處理ZeroDivisionError異常
- try:
- print(5/0)
- except ZeroDivisionError:
- print("You can't divide by zero!")
- 處理FileNotFoundError異常
- f_name = 'siddhartha.txt'
- try:
- with open(f_name) as f_obj:
- lines = f_obj.readlines()
- except FileNotFoundError:
- msg = "Can't find file {0}.".format(f_name)
- print(msg)
在編寫代碼時(shí),很難知道要處理哪種異常。嘗試編寫沒有try塊的代碼,并讓它生成一個(gè)錯(cuò)誤。回溯將告訴您程序需要處理哪種異常。
else代碼塊
try塊應(yīng)該只包含可能導(dǎo)致錯(cuò)誤的代碼。任何依賴于try塊成功運(yùn)行的代碼都應(yīng)該放在else塊中。
- 使用else塊
- print("Enter two numbers. I'll divide them.")
- x = input("First number: ")
- y = input("Second number: ")
- try:
- result = int(x) / int(y)
- except ZeroDivisionError:
- print("You can't divide by zero!")
- else:
- print(result)
- 防止用戶輸入導(dǎo)致的崩潰
如果沒有下面示例中的except塊,如果用戶試圖除零,程序?qū)⒈罎?。正如所寫的,它將?yōu)雅地處理錯(cuò)誤并繼續(xù)運(yùn)行。
- # 一個(gè)簡(jiǎn)單的除法計(jì)算器。
- print("Enter two numbers. I'll divide them.")
- print("Enter 'q' to quit.")
- while True:
- x = input("\nFirst number: ")
- if x == 'q':
- break
- y = input("Second number: ")
- if y == 'q':
- break
- try:
- result = int(x) / int(y)
- except ZeroDivisionError:
- print("You can't divide by zero!")
- else:
- print(result)
默默地失敗
有時(shí)希望程序在遇到錯(cuò)誤時(shí)繼續(xù)運(yùn)行,而不向用戶報(bào)告錯(cuò)誤。在else塊中使用pass語(yǔ)句可以做到這一點(diǎn)。
- 在else塊中使用pass語(yǔ)句
- f_names = ['alice.txt', 'siddhartha.txt',
- 'moby_dick.txt', 'little_women.txt']
- for f_name in f_names:
- # 報(bào)告找到的每個(gè)文件的長(zhǎng)度。
- try:
- with open(f_name) as f_obj:
- lines = f_obj.readlines()
- except FileNotFoundError:
- # 繼續(xù)看下一個(gè)文件。
- pass
- else:
- num_lines = len(lines)
- msg = "{0} has {1} lines.".format(
- f_name, num_lines)
- print(msg)
避免空的except塊
異常處理代碼應(yīng)該捕獲在程序執(zhí)行期間預(yù)期發(fā)生的特定異常??盏膃xcept塊將捕獲所有異常,包括在強(qiáng)制關(guān)閉程序時(shí)可能需要的鍵盤中斷和系統(tǒng)退出。
如果你想使用try塊,但又不確定要捕獲哪個(gè)異常,那么使用exception。它將捕獲大多數(shù)異常,但仍然允許您故意中斷程序。
- 避免空的except塊
- try:
- # Do something
- except:
- pass
- 使用Exception
- try:
- # Do something
- except Exception:
- pass
- 打印異常
- try:
- # Do something
- except Exception as e:
- print(e, type(e))
使用json存儲(chǔ)數(shù)據(jù)
json模塊允許您將簡(jiǎn)單的Python數(shù)據(jù)結(jié)構(gòu)轉(zhuǎn)儲(chǔ)到一個(gè)文件中,并在程序下次運(yùn)行時(shí)從該文件加載數(shù)據(jù)。JSON數(shù)據(jù)格式不是特定于Python的,所以你也可以與使用其他語(yǔ)言的人共享這類數(shù)據(jù)。
在處理存儲(chǔ)的數(shù)據(jù)時(shí),了解如何管理異常非常重要。在處理數(shù)據(jù)之前,通常希望確保試圖加載的數(shù)據(jù)存在。
- 使用json.dump()存儲(chǔ)數(shù)據(jù)
- # 存儲(chǔ)一些數(shù)字。
- import json
- numbers = [2, 3, 5, 7, 11, 13]
- filename = 'numbers.json'
- with open(filename, 'w') as f_obj:
- json.dump(numbers, f_obj)
- 使用json.load()存儲(chǔ)數(shù)據(jù)
- # 加載一些以前存儲(chǔ)的數(shù)字。
- import json
- filename = 'numbers.json'
- with open(filename) as f_obj:
- numbers = json.load(f_obj)
- print(numbers)
- 確保存儲(chǔ)的數(shù)據(jù)存在
- import json
- f_name = 'numbers.json'
- try:
- with open(f_name) as f_obj:
- numbers = json.load(f_obj)
- except FileNotFoundError:
- msg = "Can’t find {0}.".format(f_name)
- print(msg)
- else:
- print(numbers)
決定報(bào)告哪些錯(cuò)誤
編寫良好、經(jīng)過適當(dāng)測(cè)試的代碼不太容易出現(xiàn)內(nèi)部錯(cuò)誤,比如語(yǔ)法或邏輯錯(cuò)誤。但是,每當(dāng)你的程序依賴于諸如用戶輸入或文件存在之類的外部因素時(shí),就有可能引發(fā)異常。
如何將錯(cuò)誤傳達(dá)給用戶取決于你自己。有時(shí)用戶需要知道一個(gè)文件是否丟失了;有時(shí)最好是靜默地處理錯(cuò)誤。一點(diǎn)經(jīng)驗(yàn)會(huì)幫助你知道該報(bào)告多少。