Python學習筆記 檔案和異常

2021-09-22 01:50:42 字數 4039 閱讀 6084

1、從檔案中讀取資料

#關鍵字with在不需要訪問檔案後自動將其關閉

#open('pi_digits.txt')返回乙個表示檔案pi_digits.txt的物件,並將該物件儲存在變數file_object中

#file_object.read()讀取該檔案物件對應檔案的全部內容

#contents.rstrip(),由於read()讀取到檔案末尾時返回乙個空字串,rstrip()用於刪除該空字串

with

open

('pi_digits.txt'

)as file_object:

contents = file_object.read(

)print

(contents.rstrip(

))

filename =

'pi_digits.txt'

with

open

(filename)

as file_object:

for line in file_object:

print

(line.rstrip(

))

filename =

'pi_digits.txt'

#由於在with中返回的檔案物件只在with**塊中可用

#如果要在with**塊外訪問檔案的內容,可將檔案每行儲存在列表中

with

open

(filename)

as file_object:

lines = file_object.readlines(

)for line in lines:

print

(line.rstrip(

))

2、寫入檔案
filename =

'programming.txt'

#以寫入模式'w'開啟檔案,如果檔案不存在,則建立該檔案,若已存在則清空檔案內容

with

open

(filename,

'w')

as file_object:

file_object.write(

"i love you."

)

#以附加模式'a'開啟檔案,能夠在檔案末尾寫入檔案

with

open

(filename,

'a')

as file_object:

file_object.write(

"i love you.\n"

) file_object.write(

"what about you?\n"

)

3、異常

將可能引發異常的**放在try**塊中

將異常處理的**放在except**塊中

try

:print(9

/0)except zerodivisionerror:

print

("you can't divide by zero!"

)

將依賴於try**塊成功執行的**放到else**塊中

print

("give me two numbers, and i'll divide them."

)print

("enter 'q' to quit."

)while

true

: first_number =

input

("\nfirst number: "

) second_number =

input

("\nsecond number: "

)if first_number ==

'q'or second_number ==

'q':

break

try:

answer =

int(first_number)

/int

(second_number)

except zerodivisionerror:

print

("you can't divide by 0!"

)else

:print

(answer)

def

count_words

(filename)

:'''計算乙個檔案大概有多少個單詞'''

try:

with

open

(filename)

as file_object:

contents = file_object.read(

)except filenotfounderror:

# msg = "sorry, the file " + filename + " does not exist."

# print(msg)

pass

else

:#計算檔案中包含多少個單詞

words = contents.split(

) num_words =

len(words)

print

("the file "

+ filename +

" has about "

+str

(num_words)

+" words."

)#計算檔案中包含多少個'you'

you_count = words.count(

'you'

)print

("'you': "

+str

(you_count))

filename =

'alice.txt'

count_words(filename)

4、使用json儲存資料
import json

numbers =[2

,3,5

,7,11

,13]filename =

'numbers.json'

with

open

(filename,

'w')

as f_obj:

json.dump(numbers, f_obj)

import json

filename =

'numbers.json'

with

open

(filename)

as f_obj:

numbers = json.load(f_obj)

print

(numbers)

-練習

import json 

#如果以前儲存了使用者名稱,就載入它

#否則,就提示使用者輸入使用者名稱儲存它

filename =

'username.json'

try:

with

open

(filename)

as f_obj:

username = json.load(f_obj)

except filenotfounderror:

username =

input

("what's your name? "

)with

open

(filename,

'w')

as f_obj:

json.dump(username, f_obj)

print

("we'll remember you when you come back, "

+ username +

"!")

else

:print

("welcome back, "

+ username +

"!")

Python 學習筆記之檔案和異常

python中對檔案 資料夾 檔案操作函式 的操作需要涉及到os模組和shutil模組。得到當前工作目錄,即當前python指令碼工作的目錄路徑 os.getcwd 返回指定目錄下的所有檔案和目錄名 os.listdir 函式用來刪除乙個檔案 os.remove 刪除多個目錄 os.removedi...

Python檔案和異常的學習

學習 with open pi digits.txt as file object contents file object.read print contents 輸出結果 3.1415926535 8979323846 2643383279filename e code python pi di...

python錯誤和異常學習筆記

1.python中的異常 nameerror 嘗試訪問乙個未申明的變數 zerodivisionerror 除數為零 syntaxerror 直譯器語法錯誤 indexerror 請求的索引超出序列範圍 keyerror 請求乙個不存在的字典關鍵字 ioerror 輸入 輸出錯誤 attribute...