Python3 File seek () method
Outline
seek () method is used to move the file read pointer to the specified location.
grammar
seek () method has the following syntax:
fileObject.seek(offset[, whence])
parameter
offset - the start offset, which is representative of the number of bytes needed to move shift
whence: Optional, default is 0.To offset a defined parameter, which indicates the position offset from the beginning; 0 represents the start from the beginning of the file, 1 begin to run from the current position, from the end of the file represents the date.
return value
This function has no return value.
Examples
The following example demonstrates the readline () 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/python3 # 打开文件 fo = open("w3big.txt", "r+") print ("文件名为: ", fo.name) line = fo.readline() print ("读取的数据为: %s" % (line)) # 重新设置文件读取指针到开头 fo.seek(0, 0) line = fo.readline() print ("读取的数据为: %s" % (line)) # 关闭文件 fo.close()
The above example output is:
文件名为: w3big.txt 读取的数据为: 1:www.w3big.com 读取的数据为: 1:www.w3big.com