Python學習筆記(五) 檔案和異常

2021-08-09 02:43:48 字數 3154 閱讀 4305

1、從檔案中讀取資料

1、1 讀取整個檔案

with open('digits.txt') as file_object:

contents=file_object.read()

print contents.rstrip()

1)在檔案所在目錄建立檔案digits.txt後,函式open()開啟檔案儲存到變數file_object中

2)關鍵字with在不需要訪問檔案後將其關閉

3)有了檔案物件後,使用方法read()讀取整個檔案,將其作為乙個字串儲存到變數contens中。

4)read()到達檔案末尾時返回乙個空字串,而將這個空字串顯示出來時就是乙個空行。要刪除末尾的空行,可使用方法rstrip()

1、2 檔案路徑

for line in file_object:

print line.rstrip()

open()中使用絕對路徑來開啟檔案

1、3逐行讀取檔案

with open('digits.txt') as file_object:

for line in file_object:

print line.rstrip()

1)在檔案所在目錄建立檔案digits.txt後,函式open()開啟檔案儲存到變數file_object中

2)使用迴圈來遍歷檔案每一行的內容

1、4  建立乙個包含檔案各行內容的列表後使用檔案的內容

with open('digits.txt') as file_object:

lines=file_object.readlines()

new_lines=

for line in lines:

print new_lines

1)方法readlines()從檔案中讀取每一行,將其儲存在乙個列表中,該列表被儲存到變數lines中,接下來建立乙個空列表,使用迴圈遍歷列表lines,並將其元素新增到新的列表

new_lines中,列印新列表。

2)讀取文字檔案時,python將其中的所有文字都解讀為字串。如果讀取的是數字,需要使用int()將其轉換為整數

2、寫入檔案

2、1  寫入多行內容

name='a.txt'

with open(name,'w') as file_object:

file_object.write('fffffffff\n')

file_object.write('aaaaaaaaa\n')

1)函式open()抵用兩個實參,第乙個是開啟的檔名,第二個是實參'w'是寫入模式, 'r'代表讀取模式,'a'代表附加模式,'r+'代表讀取和寫入兩種模式

2)如果要寫入的檔案不存在,函式open()將自動建立他

3)以寫入模式'w'開啟檔案時,如果指定的檔案已經存在,python將在返回檔案物件前清空該檔案

4)函式write()不會再寫入文字末尾新增換行符

5)如果要新增檔案內容,而不是覆蓋原有的內容,可以附加模式開啟檔案。

name='a.txt'

with open(name,'w') as file_object:

file_object.write('fffffffff\n')

file_object.write('aaaaaaaaa\n')

with open(name,'a') as file_object:

file_object.write('bbbbbbbbb\n')

file_name='guest.txt'

while true:

with open(file_name,'a') as file_object:

message=raw_input('enter your name:')

if message=='quite':

break

file_object.write(message)

with open('guest.txt') as file_object:

print file_object.read()

3、  異常

3、1 使用try--except--else**塊

while true:

first_num=raw_input('enter first num is:')

if first_num=='q':

break

second_num=raw_input('enter second num is:')

if second_num=='q':

break

try:

result=int(first_num)/int(second_num)

except zerodivisionerror:

print 'your can not do it'

else:

print result

1)只有可能引發異常的**才需要放在try語句中

2)如果try**塊中的**執行起來沒有問題,python將跳過except**塊,若try**塊中的**導致了錯誤,python將查詢這樣的except**塊,並執行其中的**

3)依賴於try**塊成功執行的**都應該放到else**塊中

4、使用json模組來儲存資料

import json

names=['zhao','qian','sun','li','wang']

filename='name.json'

with open(filename,'w') as f_ob:

json.dump(names,f_ob)

with open(filename) as f_ob:

print json.load(f_ob)

1)函式json.dump()接受兩個實參:要儲存的資料以及用於儲存資料的檔案物件。

2)通常使用副檔名.json來指出檔案儲存的資料格式為json格式。

3)使用json.load()將檔案讀取到記憶體中。

Python學習五 檔案

with open pi digits.txt as file object contents file object.read print contents 其中,pi digits.txt 為同目錄下的文字檔案。關鍵字with在不再需要訪問檔案後將其關閉。在這個程式中,注意到我們呼叫了open ...

Python筆記 五 檔案處理

1.開啟pi digits.txt檔案 with open pi digits.txt as file object contents file object.read print contents 在read函式到達檔案末尾會返回乙個空字串,顯示出來比原始檔多乙個空行 print contents...

五 檔案操作

字元型的檔案會先編碼然後再儲存。所以讀取這些檔案的時候就需要解碼。檔案的開啟模式有唯讀 r 只寫 w 追加 a 以及二進位制模式和二進位制加下的唯讀 rb 只寫 wb 追加 ab 唯讀 預設模式,檔案必須存在,不存在則丟擲異常 只寫 不可讀 不存在則建立 存在則清空內容 追加 不可讀 不存在則建立 ...