Python File Methods

About This Index
This page is the Python File Methods index for the tutorial track. Each row links to a full guide with syntax, parameters, return value, three runnable examples, and FAQs. Every example works in a temporary folder, passes encoding="utf-8" to text files, and prints the same output on Windows, macOS, and Linux.
Learn open() modes, the with statement, text vs binary, and the classic mistakes on the File hub. Use this page when you need a specific method such as read(), seek(), or flush().
open() returns a text file (TextIOWrapper) by default, a buffered binary file (BufferedReader, BufferedWriter, or BufferedRandom) with "b" in the mode, and a raw FileIO with "b" plus buffering=0. The Works on column tells you which of them has each method.
How to Call a File Method
Open the file, call methods on the object with dot notation, and let the with block close it. The comments show what each print() outputs:
import os
import tempfile
with tempfile.TemporaryDirectory() as folder:
path = os.path.join(folder, "demo.txt")
with open(path, "w", encoding="utf-8", newline="\n") as f:
print(f.write("one\ntwo\n")) # 8
with open(path, encoding="utf-8") as f:
print(repr(f.readline())) # 'one\n'
print(f.tell()) # 4 (simple ASCII text)
print(f.readlines()) # ['two\n']
print(f.closed) # True Python File Methods Table
All method guides in this Python tutorial series (20 tutorials), grouped by purpose.
Read data
6 methodsRead the whole file, one line, a list of lines, a bounded chunk, or bytes into a buffer you own, and look ahead without consuming.
| Method | Returns | Works on | Description | Tutorial |
|---|---|---|---|---|
read() | str or bytes | Text, Binary (buffered), Raw | Read the rest of a file, or at most size characters (text) or bytes (binary). | Try it |
readline() | str or bytes | Text, Binary (buffered), Raw | Read one line, including its newline, or an empty result at end of file. | Try it |
readlines() | list | Text, Binary (buffered), Raw | Read the remaining lines into a list, optionally stopping after about hint characters or bytes. | Try it |
read1() | bytes | Binary (buffered) | Return up to size bytes using at most one read of the underlying raw stream. | Try it |
readinto() | int | Binary (buffered), Raw | Read bytes directly into an existing bytearray or memoryview and return how many were read. | Try it |
peek() | bytes | Binary (buffered) | Look at upcoming bytes in a binary buffered reader without moving the position. | Try it |
Write data
4 methodsWrite one string or bytes object, write many at once, push buffered data to the OS, or resize the file.
| Method | Returns | Works on | Description | Tutorial |
|---|---|---|---|---|
write() | int | Text, Binary (buffered), Raw | Write a string (text mode) or bytes (binary mode) and return how many were written. | Try it |
writelines() | None | Text, Binary (buffered), Raw | Write every string or bytes object from an iterable, without adding newlines. | Try it |
flush() | None | Text, Binary (buffered), Raw | Push buffered data from Python down to the operating system without closing the file. | Try it |
truncate() | int (new size) | Text, Binary (buffered), Raw | Resize a file to the current position or to a given size, without moving the position. | Try it |
Move around in a file
3 methodsJump to a position, report where you are, and check whether the stream can move at all.
| Method | Returns | Works on | Description | Tutorial |
|---|---|---|---|---|
seek() | int (new position) | Text, Binary (buffered), Raw | Move the file position to an offset from the start, the current position, or the end. | Try it |
tell() | int | Text, Binary (buffered), Raw | Return the current position: a byte offset in binary mode, an opaque cookie in text mode. | Try it |
seekable() | bool | Text, Binary (buffered), Raw | Return True if the stream supports random access with seek() and tell(). | Try it |
Check and manage the stream
7 methodsClose or unwrap a file, get its OS descriptor, ask what it allows, and change text settings while it is open.
| Method | Returns | Works on | Description | Tutorial |
|---|---|---|---|---|
close() | None | Text, Binary (buffered), Raw | Flush pending writes and release the file; calling it again does nothing. | Try it |
detach() | buffer or raw stream | Text, Binary (buffered) | Separate a text or buffered stream from the layer below it and return that layer. | Try it |
fileno() | int | Text, Binary (buffered), Raw | Return the integer file descriptor that the operating system uses for an open file. | Try it |
isatty() | bool | Text, Binary (buffered), Raw | Return True if the stream is connected to an interactive terminal. | Try it |
readable() | bool | Text, Binary (buffered), Raw | Return True if the stream was opened for reading. | Try it |
writable() | bool | Text, Binary (buffered), Raw | Return True if the stream was opened for writing. | Try it |
reconfigure() | None | Text | Change the encoding, errors, newline, or buffering settings of an open text file (Python 3.7+). | Try it |
File Attributes and Idioms
These are not methods, but you will use them next to the methods above in almost every program:
| Code | What it does |
|---|---|
with open(p, encoding="utf-8") as f: | Open a file and close it automatically when the block ends. |
for line in f: | Loop over lines, newline included; the most memory-friendly way to read text. |
print(x, file=f) | Write formatted values plus a newline, like printing to the screen. |
f.closed | True after close(); an attribute, not a method. |
f.name / f.mode | The name and the mode string passed to open(). |
f.encoding / f.errors | Text files only: the codec and error handler in use. |
f.buffer | Text files only: the binary buffer underneath the text layer. |
For a whole small file, pathlib.Path(p).read_text(encoding="utf-8") and write_text() open, read or write, and close in one call.
Frequently Asked Questions
Start with close()
Open the first guide in the list and work your way through the file methods.
15 people found this page helpful
