日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

带有示例的Python File readlines()方法

發(fā)布時間:2023/12/1 python 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 带有示例的Python File readlines()方法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文件readlines()方法 (File readlines() Method)

readlines() method is an inbuilt method in Python, it is used to get all lines from the file, the method is called with this object (current file stream/IO object) and returns all available lines in the file, we can also specify the total number of bytes to read from the line.

readlines()方法是Python中的內置方法,用于從文件中獲取所有行,該方法與此對象(當前文件流/ IO對象)一起調用,并返回文件中所有可用的行,我們還可以指定從行讀取的總字節(jié)數(shù)。

Syntax:

句法:

file_object.readlines(len)

Parameter(s):

參數(shù):

  • len – It is an optional parameter and it can be used to specify the total number of bytes to read from the file. It's default value is -1 that specifies all lines. If the len is greater than the total number of bytes of the file, then no more content will return.

    len –這是一個可選參數(shù),可用于指定要從文件讀取的總字節(jié)數(shù)。 默認值為-1,表示所有行。 如果len大于文件的字節(jié)總數(shù),則不會再返回任何內容。

Return value:

返回值:

The return type of this method is <class 'list'>, it returns the lines in the form of a list.

此方法的返回類型為<class'list'> ,它以列表形式返回行。

Example:

例:

# Python File readlines() Method with Example# creating a file myfile1 = open("hello1.txt", "w")# writing content in the file myfile1.write("Shivang, 21, Indore\n") myfile1.write("Pankaj, 27, Mumbai\n") myfile1.write("Rambha, 16, Indraloka\n") myfile1.write("Urvarshi, 18, Indraloka\n") myfile1.write("Menaka, 17, Indraloka\n")# closing the file myfile1.close()# reading the file (opening file in 'r' mode) myfile1 = open("hello1.txt","r")# reading and printing the file's content # using readlines() print("file's content (using readlines() method)...") print("myfile1.readlines()...") print(myfile1.readlines())# reading a total number of bytes # seeking file's position to 0th position myfile1.seek(0) # reads only 10 bytes print("myfile1.readlines(10)...") print(myfile1.readlines(10))# reads next 300 bytes, if no more bytes # method will not read more bytes print("myfile1.readlines(300)...") print(myfile1.readlines(300))# closing the file myfile1.close()

Output

輸出量

file's content (using readlines() method)... myfile1.readlines()... ['Shivang, 21, Indore\n', 'Pankaj, 27, Mumbai\n', 'Rambha, 16,Indraloka\n', 'Urvarshi, 18, Indraloka\n', 'Menaka, 17, Indraloka\n'] myfile1.readlines(10)... ['Shivang, 21, Indore\n'] myfile1.readlines(300)... ['Pankaj, 27, Mumbai\n', 'Rambha, 16, Indraloka\n', 'Urvarshi,18, Indraloka\n', 'Menaka, 17, Indraloka\n']

翻譯自: https://www.includehelp.com/python/file-readlines-method-with-example.aspx

總結

以上是生活随笔為你收集整理的带有示例的Python File readlines()方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。