Latest web development tutorials

Python3 File read () method

Python3 File (File) method Python3 File (File) method


Outline

read () method for reading a specified number of bytes from the file, if not given or is negative read all.

grammar

read () method has the following syntax:

fileObject.read(); 

parameter

  • size - the number of bytes read from the file.

return value

It returns the byte read from a string.

Examples

The following example illustrates the read () method of use:

W3big.txt content file as follows:

这是第一行
这是第二行
这是第三行
这是第四行
这是第五行

Loop reads the contents of the file:

#!/usr/bin/python3

# 打开文件
fo = open("w3big.txt", "r+")
print ("文件名为: ", fo.name)

line = fo.read(10)
print ("读取的字符串: %s" % (line))

# 关闭文件
fo.close()

The above example output is:

文件名为:  w3big.txt
读取的字符串: 这是第一行
这是第二

Python3 File (File) method Python3 File (File) method