Reading and Writing Files in Python



Reading data from a file and writing to a file can be easily done in Python by using the file object. The file object in Python provides a set of methods for handling files. These methods make our lives easier for interacting with files on our computer. Below, we will see how to read and write files in Python using the read() and write() methods.

The write() Method

The write() method writes any string to an opened file. It is important to note that Python strings can have binary data and not just text. This method does not add a newline character ('\n') to the end of the string.

Syntax

Following is the syntax of the Python write() method:

fileObject.write(string)

Here, the parameter string holds the content to be written into the opened file.

Writing to a File in Python

To write data to a file in Python, we initially need to open the file in the write ("w") mode using the open() function. It allows us to overwrite the existing data of the file or create a new file if the file does not exist.

Example

The following example demonstrates how to create foo.txt file and write content to that file using the open() function and write() method. Finally, we will close that file using the close() method.

# Open a file in writing mode
fo = open("foo.txt", "w") 

# Write data to the file 
fo.write( "Python is a great language.\nYeah its great!!\n") 
print ("Data written to the File successfully!!")

# Close opened file 
fo.close()

The above code produces the following result ?

Data written to the File successfully!!

If you would open this file, it will contain the following content.

Python is a great language. 
Yeah its great!!

The read() Method

The read() method reads a string from an open file. It is important to note that Python strings can have binary data apart from text data, and it reads entire data of the file.

Syntax

The syntax of the read() method is:

fileObject.read([count])

Here, passed parameter is the number of bytes to be read from the opened file. This method starts reading from the beginning of the file and if count is not specified, then it tries to read as much as possible, maybe until the end of file.

Example

Let's take a file foo.txt, which we created above, to read its content using the read() method.

# Open a file in read only mode
fo = open("foo.txt", "r") 

str = fo.read()
print("Read String is : ", str)

# Close opened file 
fo.close()

This produces the following result:

Read String is :  Python is a great language.
Yeah its great!!

Overwriting the Existing File Data

When you open an existing file in the write("w") mode, and you write new data, then the existing content of the file will be overwritten.

Example

The below example shows how to overwrite the existing file data in Python using the write() method.

with open("foo.txt", "w") as f:
     f.write("New data.\nAfter Overwriting...")

print("Overwriting the Existing Data Successfully!")

#open and read the file after the overwriting:
with open("foo.txt", "r") as f:
  print(f.read())

This produces the following result:

Overwriting the Existing Data Successfully!
New data.
After Overwriting...

Reading Lines in a File

Instead of reading the entire file data as a string, we can read lines as a list of strings using the file_object.readlines() method.

Example

The following example shows how to read lines in a file using the Python file_object.readlines() methods.

# create a new file and write lines of data
with open("My_file.txt", "w") as f:
     f.write("Data line 1.\nData line 2.\nData line 3.\nData line 4.")

print("Data written to the file Successfully!")

#open and read the file lines
with open("My_file.txt", 'r') as f:
  print("Output list of strings:")
  print(f.readlines())

This produces the following result:

Data written to the file Successfully!
Output list of strings:
['Data line 1.\n', 'Data line 2.\n', 'Data line 3.\n', 'Data line 4.']
Updated on: 2025-09-01T11:41:08+05:30

18K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements