In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to modify the file read and write position in Python, the content is concise and easy to understand, absolutely can make you shine, through the detailed introduction of this article I hope you can gain something.
Tell() method
The tell() method is used to obtain the location of reading and writing the current file. Its syntax format is as follows:
1 txt_data.tell()
Take the contents of the file txt_file.txt as an example, use the tell() method to get the location of the current file read, the example code is as follows:
1 file = open('txt_file.txt', mode='r', encoding='utf-8')
2 print(file.read(7)) #Read 7 bytes
3 print(file.tell()) #Output file read location
The above code reads 7 bytes of data using the read() method, and then looks at the current file read/write location using the tell() method.
Run Code:
1 Life is7
The seek() method
seek() method is used to set the current file read and write location, syntax format:
1 txt_data.seek(offset, from)
The parameter offset of seek() method indicates the offset, that is, the number of bytes that need to be moved from the read/write position; the parameter from is used to specify the read/write position of the file. The values of this parameter are: 0, 1, 2, and their meanings are as follows:
0: indicates reading and writing at the beginning position;
1: indicates reading and writing at the current position;
2: indicates reading and writing at the end position.
To read the contents of the file txt_file.txt for example, use the seek() method to modify the read and write location, example code:
1 file = open('txt_file.txt', mode='r',encoding='utf-8')
2 file.seek(15, 0)
3 print(file.read())
4 file.close()
The above code uses the seek() method to move the file reading position 15 bytes off the start position, and then uses the read() method to read the data in txt_file.txt.
Run the code and the result is as follows:
1 use Python.
2 Hello Python
3 Hello worldpython program development
The above content is how to modify the file reading and writing position in Python. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.