File Built-in Methods

Once open() has completed successfully and returned a file object, all subsequent access to the file transpires with that “handle.” File methods come in four different categories: input, output, movement within a file, which we will call “intra-file motion,” and miscellaneous.

Input

The read() method is used to read bytes directly into a string, reading at most the number of bytes indicated. If no size is given, the default value is set to -1, meaning that the file is read to the end. The readline() method reads one line of the open file (reads all bytes until a NEWLINE character is encountered). The NEWLINE character is retained in the returned string. The readlines() method is similar, but reads all remaining lines as strings and returns a list containing the read set of lines. The readinto() method reads the given number of bytes into a writable buffer object, the same type of object returned by the unsupported buffer() built-in function. (Since buffer() is not supported, neither is readinto() ).

Output

The write() built-in method has the opposite functionality as read() and readline(). It takes a string which can consist of one or more lines of text data or a block of bytes and writes the data to the file. writelines() operates on a list just like readlines(), but takes a list of strings and writes them out to a file. NEWLINE characters are not inserted between each line; so if desired, they must be added to the end of each line before writelines() is called.

Intra-file Motion

The seek() method (analogous to the fseek() function in C) moves the file pointer to different positions within the file. The offset in bytes is given along with a relative offset location called whence. A value of 0 indicates distance from the beginning of a file (note that a position measured from the beginning of a file is also known as the absolute offset), a value of 1 indicates movement from the current location in the file, and a value of 2 indicates that the offset is from the end of the file. If you have used fseek() as a C programmer, the values 0, 1, and 2 correspond directly to the constants SEEK_SET, SEEK_CUR, and SEEK_END, respectively. Use of the seek() method comes to play
when opening a file for read and write access.
 
tell() is a complementary method to seek(); it tells you the current location of the fileā€”in bytes from the beginning of the file.

Others

The close() method completes access to a file by closing it. The Python garbage collection routine will also close a file when the file object reference has decreased to zero. One way this can happen is when only one reference exists to a file, say, fp = open(), and fp is reassigned to another file object before the original file is explicitly closed. Good programming style suggests closing the file before reassignment to another file object.
 
The fileno() method passes back the file descriptor to the open file. This is an integer argument that can be used in lower-level operations such as those featured in the os module. The flush() method. isatty() is a Boolean built-in method that returns 1 if the file is a tty-like device and 0 otherwise. The truncate() method truncates the file to 0 or the given size bytes.