![]() ![]() # Moving the file handle to the end of the file With open(r'E:\demos\files_demos\test.txt', "r ") as fp: We will use the existing file for this operation and open a file in read and write mode. Let’s see how to move the file cursor to the end of the file. Again, we will move the file pointer to the end of the file and write more content.Next, we will move the file pointer at the start of the file and write fresh content at the beginning of the file.We will move the file pointer at the end of the file and write new content.In the below example, we will perform the following three operations.Set whence to 2 and the offset to 0 to move the file pointer to the end of the file. With open(r'E:\demos\files_demos\test.txt', "w ") as fp: After adding content, we wanted to move the pointer to the beginning of the file to read the entire file. In this example, we are writing to the text file. Let’s see how to bring the file cursor to the beginning of the file. The 0 indicates the first byte, which is the beginning of the file. We can move the file pointer to the beginning of the file using the seek() method by passing the setting whence to 0. # Moving the file handle to 6th characterĪs you can see in the output, the first six characters are missing. Text file with open(r'E:\demos\files_demos\sample.txt', "r") as fp: It means we will start reading the file directly from the 6 th character. Move it to the 5 th character from the end of the fileĬonsider the following example where we are reading a text file contents with the offset as 6. Move file pointer in the reverse direction. Move file pointer five characters behind from the current position. Move file pointer five characters ahead from the current position. Move file pointer five characters ahead from the beginning of a file. Move file pointer to the beginning of a File Refer to the below table for clear understanding. The default value for the whence is the beginning of the file, which is 0 A whence value of 2 uses the end of the file as the reference point. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |