Computer Science · Ch 2 — File Handling in Python
Text file
Text file
A text file is simply a sequence of characters — alphabets, digits, and special symbols — stored permanently on a disk. Common examples include files with extensions like .txt, .py, and .csv. When you open a text file in a text editor such as Notepad, you see readable lines of text. But internally, the file is not stored as visible characters. Instead, every character is converted into a sequence of bytes (0s and 1s) using an encoding scheme like ASCII or Unicode.
For instance, the letter 'A' is stored as the ASCII value 65, whose binary equivalent is 1000001. When a text editor reads this byte, it translates the numeric value back into the character 'A' so that a human can read it. This translation happens automatically for every character in the file.
Each line in a text file ends with a special marker called the End of Line (EOL) character. In Python, the default EOL character is the newline character \n. When a text editor or a Python interpreter encounters this EOL marker, it moves the cursor to the beginning of the next line. Other characters can also be used to indicate the end of a line, but \n is the most common in Python.
Values inside a text file are usually separated by whitespace (spaces or tabs). However, commas (,) and tab characters (\t) are also frequently used to separate data, especially in CSV (comma-separated values) files. …