f = open('Pointer test.txt','a+',encoding='utf-8') # This will create a file directly, you can view a, w, r, and the plus sign '+' and b respectively the difference # tell() show the file pointer print(f.tell()) # Change the position of the file pointer seek(offset, whence) # The offset is a number, the number of characters from whence # whence: 0: the beginning of the file 1: the current position 2: the end of the file seek(10,0) # Maybe the offset can only be negative when rb or rb+, that is, counting backwards, the author has no specific test here # f.seek(4,0) # print(f.read()) # f.seek(0,0) # f.write('aaaaa\nbbbbb\nccccc\nddddd\n') # f.seek(4,0) f.seek(0, 0) print(f.read()) print('='*10) # aaaaa\r\nbbbbb, the last two digits of this string of numbers are the same after printing from the fifth and sixth positions f.seek(6, 0) # seek to move the mouse position (digits) contains \r\n, when reading (digits) does not contain \r print(f.read(2)) # Originally, the cursor moved to the beginning 0, and the last seven after the cursor was printed, which is the same as when the cursor moved to the sixth and the one after printing print('-'*10) # The sixth position is \r, and the seventh position is \n, so reading seven does not include \r, it will print b, move six, and the one after printing is not b f.seek(5, 0) # But the movement of the cursor includes \r\n, one more bit, so the result will be different print(f.read(2)) f.close() # Supplement the number of bytes occupied by the following system when wrapping # windows \r\n \r means return to the beginning of the line\n newline # unix/linux \n # mac \r
# The contents of the 'pointer test.txt' file here are as follows:
# aaaaa
# bbbbb
# ccccc
# ddddd
file pointer position
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=325825806&siteId=291194637
Recommended
Ranking