open()
打開文件,一次傳入一行數據,能夠結合for循環和readline()來使用python
close()
用來關閉open打開的文件less
the_file = open('sketch.txt') the_file.close()
例子:ide
>>> data = open('/root/python_test/site_list.txt') >>> print(data.readline()) www.godblessyuan.com
一些基礎的目錄管理函數函數
>>> import os >>> os.getcwd() #獲取當前目錄 '/root' >>> os.chdir('/root/python_test') #切換目錄 >>> os.getcwd() '/root/python_test' >>>
>>> data = open('/root/python_test/headfirstpython/sketch.txt') >>> for each_line in data: ... (role,line_spoken) = each_line.split(':') #這裏使用idel時候,須要注意的是代碼之間的縮進 ... print role ... print line_spoken ... Other Man Anyway, I did. Man You most certainly did not! Traceback (most recent call last): File "<stdin>", line 2, in <module> ValueError: too many values to unpack
遇到報錯了,報錯意思大概是太多值致使沒有被處理,檢查發現是由於有些數據是超過一個冒號的,因此這些數據會出錯,由於split()處理不了,可是檢查了split函數的使用說明,發現是能夠支持這種狀況的,ui
>>> help(each_line.split) Help on built-in function split: split(...) S.split([sep [,maxsplit]]) -> list of strings Return a list of the words in the string S, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done. If sep is not specified or is None, any whitespace string is a separator and empty strings are removed from the result. (END)
1.檢查方式能夠參考上面的方法。this
2.支持這種狀況的參數是一個叫maxsplit的參數,若是有maxsplit的話,那麼至可能是maxsplit的數量以上的分界符纔會被處理,這樣就很好的避免太多分界符的狀況了。spa
如這樣:code
data = open('sketch.txt') for each_line in data: (role, line_spoken) = each_line.split(':', 1) print role print line_spoken data.close()
不過,即便加了參數,仍是遇到報錯了ci
Man Oh no you didn't! Other Man Oh yes I did! Man Oh look, this isn't an argument! Traceback (most recent call last): File "<stdin>", line 2, in <module> ValueError: need more than 1 value to unp
這裏是說須要超過一個值去處理,查看了數據,發現是有些數據沒有冒號致使程序處理失敗。rem
首先經過觀察find()方法對於不一樣的數據返回的值是不一樣的。
>>> each_line = 'iiiii' >>> each_line.find(':') -1 >>> each_line = 'iiiii:' >>> each_line.find(':') 5 >>>
而後可使用的邏輯有2種,一種是if判斷,另一種是try:expoet
try: 你的代碼(可能會致使運行錯誤的) except: 錯誤回覆代碼
這種方式的機制是經過捕獲某代碼的錯誤,而後執行響應的修復代碼,例子:
data = open('sketch.txt') for each_line in data: try: (role, line_spoken) = each_line.split(':', 1) print role print line_spoken except: pass data.close()
若是
(role, line_spoken) = each_line.split(':', 1) print role print line_spoken
這裏有其中一句代碼是執行失敗的,都會轉到pass裏面去,pass表明空語句,或者null,什麼也不作。
或者就是使用最簡單的if判斷
for each_line in data: if not each_line.find(':') == -1: #not關鍵字是將一個條件取反 (role, line_spoken) = each_line.split(':', 1) print(role, end='') print(' said: ', end='') print(line_spoken, end='') data.close()
不過須要注意的是,像下面這種多重try:except的代碼是很容易影響到咱們判斷那一部分代碼纔是真正有問題的代碼,由於不管裏面和外面的try出錯了,都會返回 print('The datafile is missing!'),這樣就不能判斷是那部分代碼有問題了。
try: data = open('sketch.txt') for each_line in data: try: (role, line_spoken) = each_line.split(':', 1) print role print line_spoken except: pass data.close() except: print('The datafile is missing!')
因此須要加一些標記,標識(ValueError-數據不符合指望的格式時會出現,IOError-數據沒法正常訪問時會出現。)
try: data = open('sketch.txt') for each_line in data: try: (role, line_spoken) = each_line.split(':') print role print line_spoken except ValueError: pass data.close() except IOError: print('The datafile is missing!')
http://www.godblessyuan.com/2015/04/20/head_first_python_chapter_3_lea...