Why does Python silently add a carriage return to the end of a line of text when it writes it to a text file and then just as silently removes it when it reads it from the file? The read(), readline(), and readlines() commands never show the carriage return, just a newline. However, reading the file in binary form will show that the line is actually terminated with a \r\n escape sequence, as does reading the file with a hex file utility.
If you want to read one and a half lines of text from the file with the read(size) command, you have to count the number of characters to be read, including the one \n character at the end of the first line. In this instance, you ignore the \r character, since the read(size) command never sees it.
The file pointer, however, does see both the \r and the \n characters at the end of each line of text, and is incremented for both when they are read. If you use the seek() command to position the file pointer to some place in the second line of a text file before reading some characters, you have to count the number of characters to offset the file pointer from the start of the file. Instead of just adding one for the \n at the end of the line of text, you have to add two for the \r\n pair of escape characters.
If you have to count characters, you need to make believe that there is only one escape character at the end of a line of text. If you need to figure a file pointer offset, you have to use the fact that there are actually two escape characters at the end of a text line. Is this a bug or is there some mystifying reason for this crazy operation. Look as hard as I might, I have not been able to find any documentation on this “feature”. I have found one or two tutorials on the seek() statement that used the +1 fact, but never explained it. I used my hex file utility to manually strip the \r escape characters from a text file, and all of the read commands still worked correctly, so it is rather superfluous.
Python also changes any solo \r escape characters that it finds in a text file to a \n character when it reads it. What if you really wanted the \r character?