Latest web development tutorials

Python File read () method

Python File (File) method Python 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:

1:www.w3big.com
2:www.w3big.com
3:www.w3big.com
4:www.w3big.com
5:www.w3big.com

Loop reads the contents of the file:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

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

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

# 关闭文件
fo.close()

The above example output is:

文件名为:  w3big.txt
读取的字符串: 1:www.runo

Python File (File) method Python File (File) method