12/21/23, 2:44 PM Python File I/O: Read and Write Files in Python | Python File Operations |
Search for a topic SIGNUP LOGIN
LEARN
Home > Python Guide > Tutorials > Python Files > Python File I/O
EXAMS
Python Files
Python
ASK File I/O
As a data scientist, you deal with a large amount of data on a regular basis.
CONCEPTS
And this data could come from a variety of sources, including databases,
Excel spreadsheets, flat files, and public websites such as Kaggle. Not just
sources, but any file type such as .csv, .txt, .parquet, and so on. Before you
can begin making sense of the data, you must first understand the basic
Python file operations such as how to access, read, and write data into flat
files so that you can do analytics on them.
Python, like other programming languages, enables file management and
allows users to manipulate files. The concept of file handling has spread to
many other languages, but the implementation is either complicated or
lengthy. However, like other Python innovations, this concept is simple and
short. This article will teach you about Python file operations and the basic
I/O functions available in Python. More specifically, opening a file, reading
from it, writing into it, and closing it, as well as other file techniques to be
aware of.
https://www.toppr.com/guides/python-guide/tutorials/python-files/python-file-i-o/ 1/19
12/21/23, 2:44 PM Python File I/O: Read and Write Files in Python | Python File Operations |
Table of content
1 Python File
2 Opening Files in Python
2.1 Mode
2.2 Description
3 Closing Files in Python
4 Writing to Files in Python
4.1 Python write() –
4.2 Python writelines() –
5 Reading Files in Python
5.1 Python read() –
5.2 Python readline() –
5.3 Python readlines() –
6 Python File Methods
7 Frequently Asked Questions
7.1 Q1. How do you write to a file in Python?
7.2 Q2. How do you open and write to a file in Python?
Python File
Files are identified locations on the disc where associated data is stored.
They are used to retain data in non-volatile memory indefinitely (e.g. hard
disk). Because Random Access Memory (RAM) is volatile (it loses data
when the computer is shut off), we employ files to store data for future use
by permanently storing it.
When we wish to read or write to a file, we must first open it. When we’re
finished, it needs to be closed so that the resources associated with the file
may be released. As a result, Python file operations occur in the following
order:
https://www.toppr.com/guides/python-guide/tutorials/python-files/python-file-i-o/ 2/19
12/21/23, 2:44 PM Python File I/O: Read and Write Files in Python | Python File Operations |
Opening a file
Reading or writing a file (perform operation)
Closing the file
In Python, there are two sorts of files that can be handled: text files and
binary files (written in binary language- 0s and 1s)
Binary files – The majority of files you use on a regular basis on
your computer are binary files, not text ones. That’s right, even
though it only contains text, the Microsoft Word.doc file is a binary
file. There is no line terminator in this form of a file, and the data is
kept after it has been converted into machine-readable binary
language.
Text files – A text file, on the other hand, does not require any special
encoding and may be opened with any ordinary text editor. Each line
of text in this sort of file is terminated with a special character known
as EOL (End of Line), which is the new line character (‘\n’) in Python
by default.
Opening Files in Python
To open a file, Python includes the open() function. This function returns a
file object, often known as a handle, which is used to read or change the file.
This function does not require the import of any modules.
The syntax of the Python open file function is as follows:
File_object = open(filename, access_mode)
https://www.toppr.com/guides/python-guide/tutorials/python-files/python-file-i-o/ 3/19
12/21/23, 2:44 PM Python File I/O: Read and Write Files in Python | Python File Operations |
where,
filename – name of the file that the file object has to open
access_mode – attribute of the file that tells which mode a file was
opened in. Default is reading in text mode
For example,
COPY CODE
>>> f = open('myFile.txt') # opens file in current directory
>>> f = open('C:\MyFolder\myFile.txt') # specifying full path
In this case, the filename is the name of the file with which you want to
communicate, including the file extension. That is, if you have a text file
named myFile.txt, the filename is not simply “myFile.” It’s called
“myFile.txt“. If you’re using Windows, you can also specify the file’s actual
location, such as “C:\MyFolder\myFile.txt“.
Note – The file must be in the same directory as the Python program file;
otherwise, the full location of the file must be written in place of the
filename.
When we open a file, we can define the access mode. Python understands
what you want to do with the file based on the mode parameter in the open
function. We specify whether we wish to read r, write w, or add a to the file
https://www.toppr.com/guides/python-guide/tutorials/python-files/python-file-i-o/ 4/19
12/21/23, 2:44 PM Python File I/O: Read and Write Files in Python | Python File Operations |
in mode. We can also choose whether to open the file in text or binary mode.
Reading in text mode is the default setting. When reading from a file in this
mode, we get strings. Binary mode, on the other hand, returns bytes and is
the mode to employ when dealing with non-text files such as images or
executable files.
Mode Description
r Allows you to open a file for reading. (default)
This command opens a file for writing. If the file does not exist, it is
w
created; otherwise, it is truncated.
Opens a file for the purpose of exclusive creation. The operation fails if
x
the file already exists.
Opens a file for adding at the end without truncating it. If the file does
a
not exist, it is created.
t Opens a file in text mode (default)
b Opens in binary mode.
+ Opens a file for modification (reading and writing)
One new mode has been added to Python 3:
‘x’ – Exclusive Creation Mode – This mode is used to generate a file only.
The function call will fail if a file with the same name already exists.
https://www.toppr.com/guides/python-guide/tutorials/python-files/python-file-i-o/ 5/19
12/21/23, 2:44 PM Python File I/O: Read and Write Files in Python | Python File Operations |
Few examples of opening a file in different access modes,
COPY CODE
f = open('myFile.txt') # equivalent to 'r' or 'rt'
f = open('myFile.txt','w') # write in text mode
f = open('myFile.txt', 'r+') # open a file for reading and writing
f = open('img.bmp','r+b') # read and write in binary mode
Closing Files in Python
When we have completed our operations on the file, we must properly close
it. Closing a file frees up system resources that were previously associated
with the file for I/O purposes. This allows you to manage your resources
effectively while writing software with space or memory limits.
Python’s close() function is used to do this. Python has a garbage collector
that can be used to clean away unreferenced objects, but we should not rely
on it to shut the file.
Syntax:
File_object.close()
Example
https://www.toppr.com/guides/python-guide/tutorials/python-files/python-file-i-o/ 6/19
12/21/23, 2:44 PM Python File I/O: Read and Write Files in Python | Python File Operations |
COPY CODE
f = open('myFile.txt', encoding = 'utf-8')
# perform file operations
f.close()
This procedure is not completely risk-free. If an exception occurs while
doing some operation on the file, the code departs without closing it. A try…
finally block is a safer option.
COPY CODE
try:
f = open('myFile.txt', encoding = 'utf-8')
# perform file operations
finally:
f.close()
This ensures that the file is correctly closed even if an exception is generated,
causing the program flow to halt.
When you close a file, you no longer have access to it until you reopen it at a
later time. Any attempt to read or write to a closed file object will result in a
ValueError exception:
COPY CODE
>>> f = open('/tmp/myFile.txt', 'w')
>>> f.close()
https://www.toppr.com/guides/python-guide/tutorials/python-files/python-file-i-o/ 7/19
12/21/23, 2:44 PM Python File I/O: Read and Write Files in Python | Python File Operations |
>>> f.read()
Output
Traceback (most recent call last):
File "<input>", line 1, in
f.read()
ValueError: I/O operation on closed file.
The with statement is the most effective technique to close a file. After the
nested code block is finished, this keyword automatically closes the file.
COPY CODE
with open('myFile.txt', encoding = 'utf-8') as x:
# perform file operations
If you do not use the with keyword or the fileobject.close() function, Python
will close and destroy the file object automatically using the built-in garbage
collector. As a result, it is advisable to utilize the with keyword to control
when the file will be closed.
Writing to Files in Python
Files are useless if you can’t write data to them. To accomplish this, we can
employ the Python write() function. This adds the characters you specify to
https://www.toppr.com/guides/python-guide/tutorials/python-files/python-file-i-o/ 8/19
12/21/23, 2:44 PM Python File I/O: Read and Write Files in Python | Python File Operations |
the end of a file. Keep in mind that when you create a new file object,
Python will create the file if it does not already exist. When making your
first file, you should utilize either the a+ or w+ modes. We must exercise
caution when using the w mode, as it will overwrite the file if it already
exists. As a result, all previous data is deleted.
There are two ways to enter data into a file:
1. Python write() –
Inserts the string str1 into the text file on a single line.
File_object.write(str1)
2. Python writelines() –
Each string is inserted into the text file for a list of string
elements. Used to insert many strings at once.
File_object.writelines(L) for L = [str1, str2, str3]
Example
https://www.toppr.com/guides/python-guide/tutorials/python-files/python-file-i-o/ 9/19
12/21/23, 2:44 PM Python File I/O: Read and Write Files in Python | Python File Operations |
COPY CODE
with open('Recipe.txt', 'w') as file:
file.write('5 eggs\n')
file.write('2 tsp baking powder\n')
file.write('60g butter\n')
file.write('2 cup sugar\n')
Output
5 eggs
2 tsp baking powder
60g butter
2 cup sugar
If this code is executed, the program will create a new file named recipe.txt
in the current directory if one does not already exist. It gets rewritten if it
exists. Now if we want to add more ingredients to the file, we open the file in
append mode.
COPY CODE
with open('Recipe.txt', 'a') as file:
file.write('2 cup water\n')
file.write('250ml skimmed milk\n')
https://www.toppr.com/guides/python-guide/tutorials/python-files/python-file-i-o/ 10/19
12/21/23, 2:44 PM Python File I/O: Read and Write Files in Python | Python File Operations |
Output
5 eggs
2 tsp baking powder
60g butter
2 cup sugar
2 cup water
250ml skimmed milk
Reading Files in Python
To begin reading a file in Python, open the file you wish to read. Python
requires you to specify the name and location of the file you want to open.
To read a file in Python, we must first open it in reading r mode. To read data
from a file, we can use one of three functions, which are as follows:
1. Python read() –
The fileobject.read(size) method is used to read the contents of a
file. If no size option is specified, this method will read the full file
and send it to the console as a string (in text mode) or as byte
objects (in binary mode). The size parameter instructs the read
method how many bytes to return to the display from the file.
File_object.read([n])
https://www.toppr.com/guides/python-guide/tutorials/python-files/python-file-i-o/ 11/19
12/21/23, 2:44 PM Python File I/O: Read and Write Files in Python | Python File Operations |
Let us use the above Recipe.txt file to carry out the reading Python file
operation.
Example
COPY CODE
>>> f = open('Recipe.txt','r',encoding = 'utf-8')
>>> f.read(10) # reads the first 10 data
'5 eggs\n2 t'
>>> f.read(20) # reads the next 20 data
'sp baking powder\n60g'
>>> f.read() # reads the rest of the data till the end of fil
' butter\n2 cup sugar\n2 cup water\n250ml skimmed milk\n'
2. Python readline() –
Data in a file can also be parsed by reading it line by line. This
allows you to scan an entire file line by line, progressing only when
necessary. Reads a line from a file and returns it as a string. Reads at
most n bytes for the provided n. However, even if n is greater than
the length of the line, does not read more than one line.
File_object.readline([n])
https://www.toppr.com/guides/python-guide/tutorials/python-files/python-file-i-o/ 12/19
12/21/23, 2:44 PM Python File I/O: Read and Write Files in Python | Python File Operations |
Example
COPY CODE
>>> f = open('Recipe.txt','r',encoding = 'utf-8')
>>> f.readline()
'5 eggs\n'
>>> f.readline()
'2 tsp baking powder\n'
>>> f.readline()
'60g butter\n'
>>> f.readline()
'2 cup sugar\n'
3. Python readlines() –
The fileobject.readlines() method (note the plural), which produces
a list of all the lines in the file. This method reads all the lines and
returns them as string elements in a list, one for each line.
File_object.readlines()
Example
https://www.toppr.com/guides/python-guide/tutorials/python-files/python-file-i-o/ 13/19
12/21/23, 2:44 PM Python File I/O: Read and Write Files in Python | Python File Operations |
COPY CODE
>>> f = open('Recipe.txt','r',encoding = 'utf-8')
>>> f.readlines()
['5 eggs\n', '2 tsp baking powder\n', '60g butter\n', '2 cup sugar\
'250ml skimmed milk\n']
Python File Methods
To conduct various Python file operations with the help of file objects, you
may use a variety of methods. Some of them were utilized in the above
examples. Let us look at some more Python file operation methods below:
Method Description
This function closes an open file. If the file is already closed, it has
close()
no effect.
Returns the underlying binary buffer after separating it from the
detach()
TextIOBase.
fileno() The file’s integer number (file descriptor) is returned.
flush() Flushes the file stream’s write buffer.
isatty() If the file stream is interactive, this method returns True.
Reads a maximum of n characters from the file. If it is negative or
read(n)
None, it reads till the end of the file.
https://www.toppr.com/guides/python-guide/tutorials/python-files/python-file-i-o/ 14/19
12/21/23, 2:44 PM Python File I/O: Read and Write Files in Python | Python File Operations |
readable() If the file stream can be read from, this method returns True.
Reads one line from the file and returns it. If n is supplied, it reads
readline(n=-1)
in at most n bytes.
Reads the file and returns a list of lines. If n bytes/characters are
readlines(n=-1)
given, this function reads in at most n bytes/characters.
Changes the file position in reference to from to offset bytes (start,
seek(offset,from=SEEK_SET)
current, end).
If the file stream enables random access, this function returns
seekable()
True.
tell() The current file location is returned.
The file stream is resized to size bytes. If no size is supplied, it
truncate(size=None)
resizes to the current position.
writable() If the file stream can be written to, this method returns True.
Returns the number of characters written after writing the string s
write(s)
to the file.
writelines(lines) A list of lines is written to the file.
Frequently Asked Questions
Q1. How do you write to a file in Python?
Files are useless if you can’t write data to them. To accomplish this, we can
employ the Python write() function. This adds the characters you specify to
https://www.toppr.com/guides/python-guide/tutorials/python-files/python-file-i-o/ 15/19
12/21/23, 2:44 PM Python File I/O: Read and Write Files in Python | Python File Operations |
the end of a file. Keep in mind that when you create a new file object,
Python will create the file if it does not already exist. When making your
first file, you should utilize either the a+ or w+ modes. We must exercise
caution when using the w mode, as it will overwrite the file if it already
exists. As a result, all previous data is deleted.
There are two ways to enter data into a file:
1. Python write() – Inserts the string str1 into the text file on a single
line.
COPY CODE
File_object.write(str1)
2. Python writelines() – Each string is inserted into the text file for a
list of string elements. Used to insert many strings at once.
COPY CODE
File_object.writelines(L) for L = [str1, str2, str3]
Q2. How do you open and write to a file in Python?
The open() method in Python allows you to write to a file. To write to a file,
you must supply either “w” or “a” as an argument. “w” overwrites a file’s
existing content, whereas “a” appends material to a file.
https://www.toppr.com/guides/python-guide/tutorials/python-files/python-file-i-o/ 16/19
12/21/23, 2:44 PM Python File I/O: Read and Write Files in Python | Python File Operations |
For example
COPY CODE
my_file = open('Recipe.txt', 'w')
We’ve used the w mode to open our Recipe.txt file. This means we can
make changes to the file we’re working on. You can write to a file in this
mode. It deletes the contents of a file and replaces it with a new one.
Share with friends
BROWSE
Python Files
Python Exception Handling Python Errors and Built-in Exceptions
Python File I/O Python Directory and Files Management
Customize your course in 30 seconds
Which class are you in?
5 th 6 th 7 th 8 th
9 th 10 th 11 th 12 th
https://www.toppr.com/guides/python-guide/tutorials/python-files/python-file-i-o/ 17/19
12/21/23, 2:44 PM Python File I/O: Read and Write Files in Python | Python File Operations |
GET STARTED
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best
schedule and enjoy fun and interactive classes.
Ashhar Firdausi Dr. Nazma Shaik Gaurav Tiwari
IIT Roorkee VTU APJAKTU
Biology Chemistry Physics
GET STARTED
BROWSE
Python Files
Python Exception Handling
Python Errors and Built-in Exceptions
Python File I/O
Python Directory and Files Management
Download the App
https://www.toppr.com/guides/python-guide/tutorials/python-files/python-file-i-o/ 18/19
12/21/23, 2:44 PM Python File I/O: Read and Write Files in Python | Python File Operations |
Watch lectures, practise questions and take tests on the go.
Download Previous Years QUESTION PAPERS
Quick TIPS AND TRICKS for Successful Exam Preparation
Download NCERT Notes and Solutions
Learn from VIDEO lectures
CLASSES BOARDS EXAMS
CLASS 5 CBSE MADHYA JEE MAIN NEET NTSE
PRADESH
CLASS 6 ICSE JEE ADVANCED AIIMS NSO
MAHARASHTRA
CLASS 7 IGCSE UPSEE AFMC NSTSE
PUNJAB
CLASS 8 ANDHRA BITSAT AP EAMCET KVPY
PRADESH RAJASTHAN
CLASS 9 TS EAMCET COMEDK HBBVS
BIHAR TAMIL NADU
CLASS 10 WBJEE JIPMER IMO
GUJARAT TELANGANA
CLASS 11 VITEEE BCECE IEO
JHARKHAND UTTAR PRADESH
CLASS 12 MHT-CET KCET IJSO
KARNATAKA
CLASS 12+ SAT KEAM NDA
KERALA
About Us • Press • Customer Stories • Jobs • Educators • Blog • Bytes •
Contact Us • FAQs
Terms Of Service • Privacy Policy
heap.identify('unique_identifier');
https://www.toppr.com/guides/python-guide/tutorials/python-files/python-file-i-o/ 19/19