python read(),readline(),readlines()

  文本文件:

     

hello
world!
this 
is 
a 
test
text

  read():從當前位置開始,讀完所有的文件,返回字符串。

>>> myfile = open("test.txt","r")
>>> myfile.read()
'hello\nworld!\nthis \nis \na \ntest\ntext\n'
>>> myfile.read()
''
>>> myfile.close()

    readline():只讀下一行內容,返回字符串。

>>> myfile = open("test.txt","r")
>>> myfile.readline()
'hello\n'
>>> myfile.readline()
'world!\n'
>>> myfile.readline()
'this \n'
>>> myfile.readlines()
['is \n', 'a \n', 'test\n', 'text\n']
>>> myfile.close()
     readlines():從當前位置,每次讀取多行,返回列表。

>>> myfile = open("test.txt","r")
>>> myfile.readlines()
['hello\n', 'world!\n', 'this \n', 'is \n', 'a \n', 'test\n', 'text\n']
>>> myfile.readline()
''
>>> myfile.close()




    


    

發佈了25 篇原創文章 · 獲贊 4 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章