Python File Methods

Beginner
5 min read
Updated: Nov 2026
20 method guides
Method index

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().

One function, three kinds of file object.

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:

Python
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 methods

Read the whole file, one line, a list of lines, a bounded chunk, or bytes into a buffer you own, and look ahead without consuming.

MethodReturnsWorks onDescriptionTutorial
read()str or bytesText, Binary (buffered), RawRead the rest of a file, or at most size characters (text) or bytes (binary).Try it
readline()str or bytesText, Binary (buffered), RawRead one line, including its newline, or an empty result at end of file.Try it
readlines()listText, Binary (buffered), RawRead the remaining lines into a list, optionally stopping after about hint characters or bytes.Try it
read1()bytesBinary (buffered)Return up to size bytes using at most one read of the underlying raw stream.Try it
readinto()intBinary (buffered), RawRead bytes directly into an existing bytearray or memoryview and return how many were read.Try it
peek()bytesBinary (buffered)Look at upcoming bytes in a binary buffered reader without moving the position.Try it

Write data

4 methods

Write one string or bytes object, write many at once, push buffered data to the OS, or resize the file.

MethodReturnsWorks onDescriptionTutorial
write()intText, Binary (buffered), RawWrite a string (text mode) or bytes (binary mode) and return how many were written.Try it
writelines()NoneText, Binary (buffered), RawWrite every string or bytes object from an iterable, without adding newlines.Try it
flush()NoneText, Binary (buffered), RawPush buffered data from Python down to the operating system without closing the file.Try it
truncate()int (new size)Text, Binary (buffered), RawResize a file to the current position or to a given size, without moving the position.Try it

Move around in a file

3 methods

Jump to a position, report where you are, and check whether the stream can move at all.

MethodReturnsWorks onDescriptionTutorial
seek()int (new position)Text, Binary (buffered), RawMove the file position to an offset from the start, the current position, or the end.Try it
tell()intText, Binary (buffered), RawReturn the current position: a byte offset in binary mode, an opaque cookie in text mode.Try it
seekable()boolText, Binary (buffered), RawReturn True if the stream supports random access with seek() and tell().Try it

Check and manage the stream

7 methods

Close or unwrap a file, get its OS descriptor, ask what it allows, and change text settings while it is open.

MethodReturnsWorks onDescriptionTutorial
close()NoneText, Binary (buffered), RawFlush pending writes and release the file; calling it again does nothing.Try it
detach()buffer or raw streamText, Binary (buffered)Separate a text or buffered stream from the layer below it and return that layer.Try it
fileno()intText, Binary (buffered), RawReturn the integer file descriptor that the operating system uses for an open file.Try it
isatty()boolText, Binary (buffered), RawReturn True if the stream is connected to an interactive terminal.Try it
readable()boolText, Binary (buffered), RawReturn True if the stream was opened for reading.Try it
writable()boolText, Binary (buffered), RawReturn True if the stream was opened for writing.Try it
reconfigure()NoneTextChange 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:

CodeWhat 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.closedTrue after close(); an attribute, not a method.
f.name / f.modeThe name and the mode string passed to open().
f.encoding / f.errorsText files only: the codec and error handler in use.
f.bufferText 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

They are the methods of the file objects that open() returns, such as f.read(), f.write(), f.seek(), and f.close(). They read, write, move around in, and manage an open file.
All 20 public methods of Python file objects: close, detach, fileno, flush, isatty, peek, read, read1, readable, readinto, readline, readlines, reconfigure, seek, seekable, tell, truncate, writable, write, and writelines.
open() returns different classes. Text files are TextIOWrapper objects that work with str; binary files are BufferedReader, BufferedWriter, or BufferedRandom objects that work with bytes. peek(), read1(), and readinto() only exist on the binary side, and reconfigure() only on the text side.
No. open() is a built-in function and its file objects already have the methods. Import io only for names such as io.UnsupportedOperation, io.StringIO, or io.BytesIO.
Read the Python File hub first for open() modes, the with statement, encodings, and common mistakes. Then open read(), readline(), write(), seek(), and close() from the tables below.

Start with close()

Open the first guide in the list and work your way through the file methods.

close() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful