Python3 File seek() Method | Tutorial
Tutorial -- Learning is not just about technology, but also about dreams!
- Home
- HTML
- JavaScript
- CSS
- Vue
- React
- Python3
- Java
- C
- C++
- C#
- AI
- Go
- SQL
- Linux
- VS Code
- Bootstrap
- Git
- Local Bookmarks
Python 3 Tutorial
Python3 Tutorial Python3 Introduction Python3 Environment Setup Python3 VScode Python3 Basic Syntax Python3 Basic Data Types Python3 Data Type Conversion Python3 Interpreter Python3 Comments Python3 Operators Python3 Numbers Python3 Strings Python3 Lists Python3 Tuples Python3 Dictionaries Python3 Sets Python3 Conditional Statements Python3 Loops Python3 First Program Python3 Comprehensions Python3 Iterators and Generators Python3 with Python3 Functions Python3 lambda (Anonymous Function) Python Decorators Python3 Data Structures Python3 Modules Python __name__ Python3 Input and Output Python3 File Python3 OS Python3 Errors and Exceptions Python3 Object Oriented Python3 Namespace/Scope Creating Python Virtual Environments Python Type Hints Python3 Standard Library Overview Python3 Examples Python Quiz
Python3 Advanced Tutorial
Python3 Regular Expressions Python3 CGI Programming Python3 MySQL (mysql-connector) Python3 MySQL (PyMySQL) Python3 Network Programming Python3 SMTP Sending Email Python3 Multithreading Python3 XML Processing Python3 JSON Python3 Date and Time Python3 Built-in Functions Python3 MongoDB Python3 urllib Python uWSGI Installation and Configuration Python3 pip Python3 operator Module Python math Module Python requests Module Python random Module Python OpenAI Python Useful Resources Python AI Drawing Python statistics Module Python hashlib Module Python Quantitative Trading Python pyecharts Module Python selenium Library Python Web Scraping Python Scrapy Library Python Markdown Python sys Module Python Pickle Module Python subprocess Module Python queue Module Python StringIO Module Python logging Module Python datetime Module Python re Module Python csv Module Python threading Module Python asyncio Module Python PyQt Python for Loop Python while Loop
Python3 Input and Output Python3 OS
Python3.x Python3 File seek() Method
Overview
The seek() method is used to move the file read pointer to a specified position.
Syntax
The syntax for the seek() method is as follows:
fileObject.seek(offset[, whence])
Parameters
- offset -- The starting offset, which represents the number of bytes to move. If it is a negative number, it indicates starting from the nth byte from the end.
- whence: Optional, default value is 0. Defines a parameter for offset, indicating from which position to start the offset; 0 means from the beginning of the file, 1 means from the current position, and 2 means from the end of the file.
Return Value
If the operation is successful, it returns the new file position. If the operation fails, the function returns -1.
Example
The following example demonstrates the use of the seek() method:
Example
>>> f =open('workfile','rb+')
>>> f.write(b'0123456789abcdef')
16
>>> f.seek(5)# Move to the sixth byte of the file
5
>>> f.read(1)
b'5'
>>> f.seek(-3,2)# Move to the third byte from the end of the file
13
>>> f.read(1)
b'd'
The content of the file tutorial.txt is as follows:
1:www. 2:www. 3:www. 4:www. 5:www.
Loop to read the file content:
Example
#!/usr/bin/python3# Open the file fo = open("tutorial.txt", "r+")print("Filename: ", fo.name)line = fo.readline()print("Read data: %s" % (line))# Reset the file read pointer to the beginning fo.seek(0, 0)line = fo.readline()print("Read data: %s" % (line))# Close the file fo.close()
The output of the above example is:
Filename: tutorial.txt Read data: 1:www. Read data: 1:www.
YouTip