Skip to content

File Methods

File Methods

This tutorial will cover the different methods that can be used to read and write to files.

Table of Methods

S.No.MethodDescriptionExample
1open()Opens a file and returns a file objectfile = open("file.txt", "r")file = open("file.txt", "r")
2close()Closes a filefile.close()file.close()
3read()Reads the entire file and returns a stringfile.read()file.read()
4readline()Reads a single line from the filefile.readline()file.readline()
5readlines()Reads all the lines from the file and returns a listfile.readlines()file.readlines()
6write()Writes a string to the filefile.write("Hello World")file.write("Hello World")
7writelines()Writes a list of strings to the filefile.writelines(["Hello", "World"])file.writelines(["Hello", "World"])
8seek()Changes the current position of the file pointerfile.seek(0)file.seek(0)
9tell()Returns the current position of the file pointerfile.tell()file.tell()
10truncate()Truncates the file to the given sizefile.truncate(10)file.truncate(10)
11flush()Flushes the internal bufferfile.flush()file.flush()
12fileno()Returns the file descriptorfile.fileno()file.fileno()
13isatty()Returns True if the file is connected to a terminalfile.isatty()file.isatty()

File Modes

The file modes are used to specify the purpose of opening a file. The following table lists the different file modes available in Python.

S.No.ModeDescription
1rrOpens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.
2rbrbOpens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode.
3r+r+Opens a file for both reading and writing. The file pointer placed at the beginning of the file.
4rb+rb+Opens a file for both reading and writing in binary format. The file pointer placed at the beginning of the file.
5wwOpens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
6wbwbOpens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
7w+w+Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
8wb+wb+Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
9aaOpens a file for appending. The file pointer is at the end of the file if the file exists. If the file does not exist, it creates a new file for writing.
10ababOpens a file for appending in binary format. The file pointer is at the end of the file if the file exists. If the file does not exist, it creates a new file for writing.
11a+a+Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. If the file does not exist, it creates a new file for reading and writing.
12ab+ab+Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. If the file does not exist, it creates a new file for reading and writing.
13xxCreates a new file. If the file already exists, the operation fails.
14x+x+Creates a new file. If the file already exists, the operation fails.
15xbxbCreates a new file in binary format. If the file already exists, the operation fails.
16xb+xb+Creates a new file in binary format. If the file already exists, the operation fails.
17ttOpens in text mode. (default)
18bbOpens in binary mode.
19++Opens a file for updating (reading and writing)
20UUUniversal newline mode.
21rUrUOpens a file for reading in universal newline mode. (deprecated)
22wUwUOpens a file for writing in universal newline mode. (deprecated)
23rbUrbUOpens a file for reading in universal newline mode. (deprecated)
24wbUwbUOpens a file for writing in universal newline mode. (deprecated)

File Object Attributes

The following table lists the different file object attributes available in Python.

S.No.AttributeDescription
1closedclosedReturns True if the file is closed, False otherwise.
2encodingencodingReturns the encoding of the file.
3modemodeReturns the mode of the file.
4namenameReturns the name of the file.
5newlinesnewlinesReturns a tuple of all the line terminators in the file.
6softspacesoftspaceReturns False if space explicitly required with print, True otherwise.

open() Method

The open() method opens a file and returns a file object. The following example opens a file named “file.txt” in read mode and prints its content.

file.txt
Hello World
file.txt
Hello World
open.py
file = open("file.txt", "r")
 
print(file.read())
open.py
file = open("file.txt", "r")
 
print(file.read())

Output:

command
C:\Users\username>python open.py
Hello World
command
C:\Users\username>python open.py
Hello World

In the above example, we open a file named “file.txt” in read mode. The file object is stored in the variable file. The read() method reads the entire content of the file and returns it as a string.

close() Method

The close() method closes a file. A closed file cannot be read or written any more. Any operation, which requires that the file be opened will raise a ValueError after the file has been closed. Calling close() more than once is allowed.

file.txt
Hello World
file.txt
Hello World
close.py
file = open("file.txt", "r")
 
print(file.read())
file.close()
close.py
file = open("file.txt", "r")
 
print(file.read())
file.close()

Output:

command
C:\Users\username>python close.py
Hello World
command
C:\Users\username>python close.py
Hello World

In the above example, we open a file named “file.txt” in read mode. The file object is stored in the variable file. The read() method reads the entire content of the file and returns it as a string. The close() method closes the file.

read() Method

The read() method reads the entire content of the file and returns it as a string. The following example opens a file named “file.txt” in read mode and prints its content.

file.txt
Hello World
file.txt
Hello World
read.py
file = open("file.txt", "r")
 
print(file.read())
read.py
file = open("file.txt", "r")
 
print(file.read())

Output:

command
C:\Users\username>python read.py
Hello World
command
C:\Users\username>python read.py
Hello World

In the above example, we open a file named “file.txt” in read mode. The file object is stored in the variable file. The read() method reads the entire content of the file and returns it as a string.

readline() Method

The readline() method reads a single line from the file. The following example opens a file named “file.txt” in read mode and prints its first line.

file.txt
Hello World
file.txt
Hello World
readline.py
file = open("file.txt", "r")
 
print(file.readline())
readline.py
file = open("file.txt", "r")
 
print(file.readline())

Output:

command
C:\Users\username>python readline.py
Hello World
command
C:\Users\username>python readline.py
Hello World

In the above example, we open a file named “file.txt” in read mode. The file object is stored in the variable file. The readline() method reads the first line of the file and returns it as a string.

readlines() Method

The readlines() method reads all the lines from the file and returns a list. The following example opens a file named “file.txt” in read mode and prints all its lines.

file.txt
Hello World
Hii World
file.txt
Hello World
Hii World
readlines.py
file = open("file.txt", "r")
 
print(file.readlines())
readlines.py
file = open("file.txt", "r")
 
print(file.readlines())

Output:

command
C:\Users\username>python readlines.py
['Hello World\n', 'Hii World']
command
C:\Users\username>python readlines.py
['Hello World\n', 'Hii World']

In the above example, we open a file named “file.txt” in read mode. The file object is stored in the variable file. The readlines() method reads all the lines from the file and returns a list.

write() Method

The write() method writes a string to the file. The following example opens a file named “file.txt” in write mode and writes a string to it.

write.py
file = open("file.txt", "w")
 
file.write("Hello World")
write.py
file = open("file.txt", "w")
 
file.write("Hello World")

Output:

command
C:\Users\username>python write.py
command
C:\Users\username>python write.py
file.txt
Hello World
file.txt
Hello World

In the above example, we open a file named “file.txt” in write mode. The file object is stored in the variable file. The write() method writes a string to the file.

writelines() Method

The writelines() method writes a list of strings to the file. The following example opens a file named “file.txt” in write mode and writes a list of strings to it.

writelines.py
file = open("file.txt", "w")
 
file.writelines(["Hello", "World"])
writelines.py
file = open("file.txt", "w")
 
file.writelines(["Hello", "World"])

Output:

command
C:\Users\username>python writelines.py
command
C:\Users\username>python writelines.py
file.txt
HelloWorld
file.txt
HelloWorld

In the above example, we open a file named “file.txt” in write mode. The file object is stored in the variable file. The writelines() method writes a list of strings to the file.

seek() Method

The seek() method changes the current position of the file pointer. The following example opens a file named “file.txt” in read mode and changes the current position of the file pointer.

file.txt
Hello World
file.txt
Hello World
seek.py
file = open("file.txt", "r")
 
file.seek(6)
print(file.read())
seek.py
file = open("file.txt", "r")
 
file.seek(6)
print(file.read())

Output:

command
C:\Users\username>python seek.py
World
command
C:\Users\username>python seek.py
World

In the above example, we open a file named “file.txt” in read mode. The file object is stored in the variable file. The seek() method changes the current position of the file pointer to 6. The read() method reads the content of the file from the current position of the file pointer.

tell() Method

The tell() method returns the current position of the file pointer. The following example opens a file named “file.txt” in read mode and prints the current position of the file pointer.

file.txt
Hello World
file.txt
Hello World
tell.py
file = open("file.txt", "r")
 
file.seek(6)
print(file.tell())
tell.py
file = open("file.txt", "r")
 
file.seek(6)
print(file.tell())

Output:

command
C:\Users\username>python tell.py
6
command
C:\Users\username>python tell.py
6

In the above example, we open a file named “file.txt” in read mode. The file object is stored in the variable file. The seek() method changes the current position of the file pointer to 6. The tell() method returns the current position of the file pointer.

truncate() Method

The truncate() method truncates the file to the given size. The following example opens a file named “file.txt” in read mode and truncates it to 10 bytes.

file.txt
Hello World
file.txt
Hello World
truncate.py
file = open("file.txt", "r+")
 
file.truncate(10)
print(file.read())
truncate.py
file = open("file.txt", "r+")
 
file.truncate(10)
print(file.read())

Output:

command
C:\Users\username>python truncate.py
Hello Wor
command
C:\Users\username>python truncate.py
Hello Wor

In the above example, we open a file named “file.txt” in read mode. The file object is stored in the variable file. The truncate() method truncates the file to 10 bytes. The read() method reads the content of the file.

flush() Method

The flush() method flushes the internal buffer. The following example opens a file named “file.txt” in write mode and flushes the internal buffer.

flush.py
file = open("file.txt", "w")
 
file.write("Hello World")
file.flush()
flush.py
file = open("file.txt", "w")
 
file.write("Hello World")
file.flush()

Output:

command
C:\Users\username>python flush.py
command
C:\Users\username>python flush.py
file.txt
Hello World
file.txt
Hello World

In the above example, we open a file named “file.txt” in write mode. The file object is stored in the variable file. The write() method writes a string to the file. The flush() method flushes the internal buffer.

fileno() Method

The fileno() method returns the file descriptor. The following example opens a file named “file.txt” in read mode and prints its file descriptor.

file.txt
Hello World
file.txt
Hello World
fileno.py
file = open("file.txt", "r")
 
print(file.fileno())
fileno.py
file = open("file.txt", "r")
 
print(file.fileno())

Output:

command
C:\Users\username>python fileno.py
3
command
C:\Users\username>python fileno.py
3

In the above example, we open a file named “file.txt” in read mode. The file object is stored in the variable file. The fileno() method returns the file descriptor.

isatty() Method

The isatty() method returns True if the file is connected to a terminal, False otherwise. The following example opens a file named “file.txt” in read mode and prints True if the file is connected to a terminal, False otherwise.

file.txt
Hello World
file.txt
Hello World
isatty.py
file = open("file.txt", "r")
 
print(file.isatty())
isatty.py
file = open("file.txt", "r")
 
print(file.isatty())

Output:

command
C:\Users\username>python isatty.py
False
command
C:\Users\username>python isatty.py
False

In the above example, we open a file named “file.txt” in read mode. The file object is stored in the variable file. The isatty() method returns False because the file is not connected to a terminal.

closed Attribute

The closed attribute returns True if the file is closed, False otherwise. The following example opens a file named “file.txt” in read mode and prints True if the file is closed, False otherwise.

file.txt
Hello World
file.txt
Hello World
closed.py
file = open("file.txt", "r")
 
print(file.closed)
file.close()
print(file.closed)
closed.py
file = open("file.txt", "r")
 
print(file.closed)
file.close()
print(file.closed)

Output:

command
C:\Users\username>python closed.py
False
True
command
C:\Users\username>python closed.py
False
True

In the above example, we open a file named “file.txt” in read mode. The file object is stored in the variable file. The closed attribute returns False because the file is not closed. The close() method closes the file. The closed attribute returns True because the file is closed.

encoding Attribute

The encoding attribute returns the encoding of the file. The following example opens a file named “file.txt” in read mode and prints its encoding.

file.txt
Hello World
file.txt
Hello World
encoding.py
file = open("file.txt", "r")
 
print(file.encoding)
encoding.py
file = open("file.txt", "r")
 
print(file.encoding)

Output:

command
C:\Users\username>python encoding.py
cp1252
command
C:\Users\username>python encoding.py
cp1252

In the above example, we open a file named “file.txt” in read mode. The file object is stored in the variable file. The encoding attribute returns the encoding of the file.

mode Attribute

The mode attribute returns the mode of the file. The following example opens a file named “file.txt” in read mode and prints its mode.

file.txt
Hello World
file.txt
Hello World
mode.py
file = open("file.txt", "r")
 
print(file.mode)
mode.py
file = open("file.txt", "r")
 
print(file.mode)

Output:

command
C:\Users\username>python mode.py
r
command
C:\Users\username>python mode.py
r

In the above example, we open a file named “file.txt” in read mode. The file object is stored in the variable file. The mode attribute returns the mode of the file.

name Attribute

The name attribute returns the name of the file. The following example opens a file named “file.txt” in read mode and prints its name.

file.txt
Hello World
file.txt
Hello World
name.py
file = open("file.txt", "r")
 
print(file.name)
name.py
file = open("file.txt", "r")
 
print(file.name)

Output:

command
C:\Users\username>python name.py
file.txt
command
C:\Users\username>python name.py
file.txt

In the above example, we open a file named “file.txt” in read mode. The file object is stored in the variable file. The name attribute returns the name of the file.

newlines Attribute

The newlines attribute returns a tuple of all the line terminators in the file. The following example opens a file named “file.txt” in read mode and prints its line terminators.

file.txt
Hello World
Hii World
file.txt
Hello World
Hii World
newlines.py
file = open("file.txt", "r")
 
print(file.newlines)
newlines.py
file = open("file.txt", "r")
 
print(file.newlines)

Output:

command
C:\Users\username>python newlines.py
('\r\n', '\r\n')
command
C:\Users\username>python newlines.py
('\r\n', '\r\n')

In the above example, we open a file named “file.txt” in read mode. The file object is stored in the variable file. The newlines attribute returns a tuple of all the line terminators in the file.

softspace Attribute

The softspace attribute returns False if space explicitly required with print, True otherwise. The following example opens a file named “file.txt” in read mode and prints False if space explicitly required with print, True otherwise.

file.txt
Hello World
file.txt
Hello World
softspace.py
file = open("file.txt", "r")
 
print(file.softspace)
print("Hello World")
print(file.softspace)
softspace.py
file = open("file.txt", "r")
 
print(file.softspace)
print("Hello World")
print(file.softspace)

Output:

command
C:\Users\username>python softspace.py
False
Hello World
True
command
C:\Users\username>python softspace.py
False
Hello World
True

In the above example, we open a file named “file.txt” in read mode. The file object is stored in the variable file. The softspace attribute returns False because space explicitly required with print. The print() function prints “Hello World”. The softspace attribute returns True because space is not explicitly required with print.

Conclusion

In this tutorial, you learned about the different methods that can be used to read and write to files. In the next tutorial, you will learn about the different file attributes that can be used to get information about a file. For more information on the file methods, refer to the official documentation. For more tutorials, visit our Python Central Hub.

Was this page helpful?

Let us know how we did