Skip to content

Write and Read File

Writing Files in Python

Writing files in Python is a fundamental skill that allows you to store and manipulate data. Whether youโ€™re creating new files, appending content to existing ones, or writing binary data, Python provides versatile methods for file writing. In this comprehensive guide, weโ€™ll explore various aspects of writing files in Python.

Opening a File in Write Mode

We can open a file in write mode using the open()open() function. This function accepts two arguments, the name of the file and the mode in which we want to open the file. The mode can be read mode, write mode, append mode, etc. The default mode is read mode.

Syntax:

open.py
file_object = open(file_name, [access_mode], [buffering])
open.py
file_object = open(file_name, [access_mode], [buffering])

Here,

  • file_namefile_name is the name of the file that we want to access.
  • access_modeaccess_mode is the mode in which we want to open the file, i.e., read, write, append, etc.
  • bufferingbuffering is an optional integer used to set the buffering policy.
  • file_objectfile_object is the object that we can use to access the file.

File Opening Write Modes

There are different modes in which we can open a file. These modes are:

S.No.ModeDescription
1wwOpens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
2wbwbOpens 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.
3w+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.
4wb+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.
5aaOpens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
6ababOpens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
7a+a+Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.
8ab+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. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.

Writing to a File in Python

We can write to a file in Python using the write()write() method. This method writes a string to the file. The string can be a string literal or a variable that contains a string.

Syntax:

write.py
file_object.write(string)
write.py
file_object.write(string)

Here, stringstring is the string data that we want to write to the file.

Example: Writing to a File

write.py
# Open a file in write mode
file_object = open("file.txt", "w")
 
# Write to the file
file_object.write("Hello World")
 
# Close the file
file_object.close()
write.py
# Open a file in write mode
file_object = open("file.txt", "w")
 
# Write to the file
file_object.write("Hello World")
 
# Close the file
file_object.close()

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 in write mode using the open()open() function. Then, we write the string "Hello World""Hello World" to the file using the write()write() method. Finally, we close the file using the close()close() method.

Writing Multiple Lines to a File

We can write multiple lines to a file using the write()write() method. To do so, we need to add a newline character (\n\n) at the end of each line.

Example: Writing Multiple Lines to a File

write.py
# Open a file in write mode
file_object = open("file.txt", "w")
 
# Write to the file
file_object.write("Hello World\n")
 
# Write another line to the file
file_object.write("This is a test file\n")
 
# Close the file
file_object.close()
write.py
# Open a file in write mode
file_object = open("file.txt", "w")
 
# Write to the file
file_object.write("Hello World\n")
 
# Write another line to the file
file_object.write("This is a test file\n")
 
# Close the file
file_object.close()

Output:

command
C:\Users\username>python write.py
command
C:\Users\username>python write.py
file.txt
Hello World
This is a test file
file.txt
Hello World
This is a test file

In the above example, we write two lines to the file. We add a newline character (\n\n) at the end of each line to write the lines to the file.

Writing a List to a File

We can write a list to a file using the write()write() method. To do so, we need to convert the list to a string using the str()str() function.

Example: Writing a List to a File

write.py
# Open a file in write mode
file_object = open("file.txt", "w")
 
# Create a list
my_list = ["Hello", "World", "This", "is", "a", "test", "file"]
 
# Write to the file
file_object.write(str(my_list))
 
# Close the file
file_object.close()
write.py
# Open a file in write mode
file_object = open("file.txt", "w")
 
# Create a list
my_list = ["Hello", "World", "This", "is", "a", "test", "file"]
 
# Write to the file
file_object.write(str(my_list))
 
# Close the file
file_object.close()

Output:

command
C:\Users\username>python write.py
command
C:\Users\username>python write.py
file.txt
['Hello', 'World', 'This', 'is', 'a', 'test', 'file']
file.txt
['Hello', 'World', 'This', 'is', 'a', 'test', 'file']

In the above example, we create a list and write it to the file. We convert the list to a string using the str()str() function before writing it to the file.

Writing to a File Using the with Statement

We can also write to a file using the withwith statement. This statement creates a temporary variable (file_objectfile_object in the example below) that we can use to access the file inside the indented block of the withwith statement. When we exit the withwith statement, the file is automatically closed.

Example: Writing to a File Using the with Statement

with.py
# Open a file in write mode
with open("file.txt", "w") as file_object:
    # Write to the file
    file_object.write("Hello World")
 
# The file is automatically closed
with.py
# Open a file in write mode
with open("file.txt", "w") as file_object:
    # Write to the file
    file_object.write("Hello World")
 
# The file is automatically closed

Output:

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

In the above example, we open a file in write mode using the withwith statement. Then, we write the string "Hello World""Hello World" to the file using the write()write() method. Finally, we exit the withwith statement, and the file is automatically closed.

Appending to a File in Python

We can append to a file in Python using the write()write() method. This method writes a string to the file. The string can be a string literal or a variable that contains a string. We need to use aa as the mode while opening the file.

Syntax:

append.py
file_object = open(file_name, "a")
file_object.write(string)
append.py
file_object = open(file_name, "a")
file_object.write(string)

Here,

  • file_namefile_name is the name of the file that we want to access.
  • stringstring is the string data that we want to write to the file.
  • file_objectfile_object is the object that we can use to access the file.
  • aa is the mode in which we want to open the file, i.e., append mode.

Example: Appending to a File There is a file named file.txtfile.txt with the following content:

file.txt
Hello World
file.txt
Hello World
append.py
# Open a file in append mode
file_object = open("file.txt", "a")
 
# Write to the file
file_object.write("\nThis is a test file")
 
# Close the file
file_object.close()
append.py
# Open a file in append mode
file_object = open("file.txt", "a")
 
# Write to the file
file_object.write("\nThis is a test file")
 
# Close the file
file_object.close()

Output:

command
C:\Users\username>python append.py
command
C:\Users\username>python append.py
file.txt
Hello World
This is a test file
file.txt
Hello World
This is a test file

In the above example, we open a file in append mode using the open()open() function. Then, we write the string "Hello World""Hello World" to the file using the write()write() method. Finally, we close the file using the close()close() method.

Appending Multiple Lines to a File

We can append multiple lines to a file using the write()write() method. To do so, we need to add a newline character (\n\n) at the end of each line.

Example: Appending Multiple Lines to a File There is a file named file.txtfile.txt with the following content:

file.txt
This is a head
file.txt
This is a head
append.py
# Open a file in append mode
file_object = open("file.txt", "a")
 
# Write to the file
file_object.write("\nHello World\n")
 
# Write another line to the file
file_object.write("This is a test file\n")
 
# Close the file
file_object.close()
append.py
# Open a file in append mode
file_object = open("file.txt", "a")
 
# Write to the file
file_object.write("\nHello World\n")
 
# Write another line to the file
file_object.write("This is a test file\n")
 
# Close the file
file_object.close()

Output:

command
C:\Users\username>python append.py
command
C:\Users\username>python append.py
file.txt
This is a head
Hello World
This is a test file
file.txt
This is a head
Hello World
This is a test file

In the above example, we write two lines to the file. We add a newline character (\n\n) at the end of each line to write the lines to the file.

Writing in Binary Mode

We can write to a file in binary mode using the write()write() method. This method writes a string to the file. The string can be a string literal or a variable that contains a string. We need to use wbwb as the mode while opening the file.

Syntax:

binary.py
file_object = open(file_name, "wb")
file_object.write(string)
binary.py
file_object = open(file_name, "wb")
file_object.write(string)

Here,

  • file_namefile_name is the name of the file that we want to access.
  • stringstring is the string data that we want to write to the file.
  • file_objectfile_object is the object that we can use to access the file.
  • wbwb is the mode in which we want to open the file, i.e., binary mode.
  • The write()write() method writes a string to the file.

Example: Writing in Binary Mode

binary.py
# Open a file in binary mode
file_object = open("file.txt", "wb")
 
# Write to the file
file_object.write("Hello World".encode())
 
# Close the file
file_object.close()
binary.py
# Open a file in binary mode
file_object = open("file.txt", "wb")
 
# Write to the file
file_object.write("Hello World".encode())
 
# Close the file
file_object.close()

Output:

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

In the above example, we open a file in binary mode using the open()open() function. Then, we write the string "Hello World""Hello World" to the file using the write()write() method. Finally, we close the file using the close()close() method.

Using w+ Mode to Write to a File

We can use the w+w+ mode to write to a file. This mode opens the file for both writing and reading. It overwrites the existing file if the file exists. If the file does not exist, it creates a new file for reading and writing.

seek() Method

We can use the seek()seek() method to move the file pointer to a specific position in the file. This method accepts two arguments, the offset and the position. The position is optional and defaults to 00.

Syntax:

seek.py
file_object.seek(offset, [position])
seek.py
file_object.seek(offset, [position])

Here,

  • file_objectfile_object is the file object that we want to read.
  • offsetoffset is the number of bytes to be moved.
  • positionposition is the reference position from where the bytes are to be moved. It is optional and defaults to 00.
  • The seek()seek() method moves the file pointer to a specific position in the file.
  • The tell()tell() method returns the current position of the file pointer.
  • The write()write() method writes a string to the file.

Example: Using seek() Method

seek.py
# Open a file in write mode
file_object = open("file.txt", "w+")
 
# Write to the file
file_object.write("Hello World")
 
# Move the file pointer to the word "World"
file_object.seek(6, 0) # the second argument is optional and defaults to 0. It uses the absolute file positioning.
 
# Read the file
data = file_object.read(5)
 
# Print the data
print(data)
 
# Usage of tell() method
print(file_object.tell())
seek.py
# Open a file in write mode
file_object = open("file.txt", "w+")
 
# Write to the file
file_object.write("Hello World")
 
# Move the file pointer to the word "World"
file_object.seek(6, 0) # the second argument is optional and defaults to 0. It uses the absolute file positioning.
 
# Read the file
data = file_object.read(5)
 
# Print the data
print(data)
 
# Usage of tell() method
print(file_object.tell())

Output:

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

In the above example, we open a file in write mode using the open()open() function. Then, we write the string "Hello World""Hello World" to the file using the write()write() method. Next, we move the file pointer to the word "World""World" using the seek()seek() method. Then, we read the next five characters using the read()read() method. Finally, we print the data and the current position of the file pointer using the tell()tell() method. :::

Example: Using seek() Method

seek.py
# Open a file in write mode
file_object = open("file.txt", "w+")
 
# Write to the file
file_object.write("Hello World")
 
# Move the file pointer to the word "World"
file_object.seek(6)
 
# Read the file
data = file_object.read(5)
 
# Change the word "World" to "Python"
file_object.seek(6)
file_object.write("Python")
 
# Move the file pointer to the beginning of the file
file_object.seek(0)
seek.py
# Open a file in write mode
file_object = open("file.txt", "w+")
 
# Write to the file
file_object.write("Hello World")
 
# Move the file pointer to the word "World"
file_object.seek(6)
 
# Read the file
data = file_object.read(5)
 
# Change the word "World" to "Python"
file_object.seek(6)
file_object.write("Python")
 
# Move the file pointer to the beginning of the file
file_object.seek(0)

Output:

command
C:\Users\username>python seek.py
command
C:\Users\username>python seek.py
file.txt
Hello Python
file.txt
Hello Python

In the above example, we open a file in write mode using the open()open() function. Then, we write the string "Hello World""Hello World" to the file using the write()write() method. Next, we move the file pointer to the word "World""World" using the seek()seek() method. Then, we read the next five characters using the read()read() method. Next, we move the file pointer to the word "World""World" using the seek()seek() method. Then, we write the string "Python""Python" to the file using the write()write() method. Finally, we move the file pointer to the beginning of the file using the seek()seek() method.

Reading Files in Python

Reading files in Python is a fundamental operation that allows you to retrieve and process data stored in files. Whether youโ€™re dealing with text files, binary files, or reading line by line, Python provides versatile methods for file reading. In this comprehensive guide, weโ€™ll explore various aspects of reading files in Python.

Syntax:

read.py
file_object.read([size])
read.py
file_object.read([size])

Here,

  • file_objectfile_object is the file object that we want to read.
  • sizesize is an optional numeric argument. When it is provided, it reads that many characters from the file. If the size parameter is not specified, it reads and returns up to the end of the file.
  • The read()read() method returns the specified number of bytes from the file. If we omit the size argument, it returns and displays all the data from the file.

Opening a File in Write Mode

We can open a file in write mode using the open()open() function. This function accepts two arguments, the name of the file and the mode in which we want to open the file. The mode can be read mode, write mode, append mode, etc. The default mode is read mode.

Syntax:

open.py
file_object = open(file_name, [access_mode], [buffering])
open.py
file_object = open(file_name, [access_mode], [buffering])

Here,

  • file_namefile_name is the name of the file that we want to access.
  • access_modeaccess_mode is the mode in which we want to open the file, i.e., read, write, append, etc.
  • bufferingbuffering is an optional integer used to set the buffering policy.
  • file_objectfile_object is the object that we can use to access the file.

File Opening Modes

There are different modes in which we can open a file. These modes are:

S.No.ModeDescription
1rrOpens a file in read-only mode. The file pointer is placed at the beginning of the file. This is the default mode.
2rbrbOpens a file in read-only mode 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.

Example: Reading a File There is a file named file.txtfile.txt with the following content:

file.txt
Hello World
This is a test file
file.txt
Hello World
This is a test file
read.py
# Open a file in read mode
file_object = open("file.txt", "r")
 
# Read the file
data = file_object.read()
 
# Close the file
file_object.close()
 
# Print the data
print(data)
read.py
# Open a file in read mode
file_object = open("file.txt", "r")
 
# Read the file
data = file_object.read()
 
# Close the file
file_object.close()
 
# Print the data
print(data)

Output:

command
C:\Users\username>python read.py
Hello World
This is a test file
command
C:\Users\username>python read.py
Hello World
This is a test file

In the above example, we open a file in read mode using the open()open() function. Then, we read the file using the read()read() method. Finally, we close the file using the close()close() method.

Reading a File Line by Line

We can read a file line by line using the readline()readline() method. This method reads a string from the file. The string can be a string literal or a variable that contains a string.

Syntax:

readline.py
file_object.readline()
readline.py
file_object.readline()

Here,

  • file_objectfile_object is the file object that we want to read.
  • The readline()readline() method returns the next line from the file.
  • The readlines()readlines() method returns a list of all the lines in the file.

Example: Reading a File Line by Line There is a file named file.txtfile.txt with the following content:

file.txt
Hello World
This is a test file
file.txt
Hello World
This is a test file
readline.py
# Open a file in read mode
file_object = open("file.txt", "r")
 
# Read the file line by line
line1 = file_object.readline()
line2 = file_object.readline()
 
# Close the file
file_object.close()
 
# Print the data
print(line1)
print(line2)
readline.py
# Open a file in read mode
file_object = open("file.txt", "r")
 
# Read the file line by line
line1 = file_object.readline()
line2 = file_object.readline()
 
# Close the file
file_object.close()
 
# Print the data
print(line1)
print(line2)

Output:

command
C:\Users\username>python readline.py
Hello World
This is a test file
command
C:\Users\username>python readline.py
Hello World
This is a test file

In the above example, we open a file in read mode using the open()open() function. Then, we read the file line by line using the readline()readline() method. Finally, we close the file using the close()close() method.

Example: Reading a File Line by Line using readlines() Method There is a file named file.txtfile.txt with the following content:

file.txt
Hello World
This is a test file
file.txt
Hello World
This is a test file
readlines.py
# Open a file in read mode
file_object = open("file.txt", "r")
 
# Read the file line by line
lines = file_object.readlines()
 
# Close the file
file_object.close()
 
# Print the data
print(lines)
readlines.py
# Open a file in read mode
file_object = open("file.txt", "r")
 
# Read the file line by line
lines = file_object.readlines()
 
# Close the file
file_object.close()
 
# Print the data
print(lines)

Output:

command
C:\Users\username>python readlines.py
['Hello World\n', 'This is a test file\n']
command
C:\Users\username>python readlines.py
['Hello World\n', 'This is a test file\n']

In the above example, we open a file in read mode using the open()open() function. Then, we read the file line by line using the readlines()readlines() method. Finally, we close the file using the close()close() method.

Reading a File Using the with Statement

We can also read a file using the withwith statement. This statement creates a temporary variable (file_objectfile_object in the example below) that we can use to access the file inside the indented block of the withwith statement. When we exit the withwith statement, the file is automatically closed.

Example: Reading a File Using the with Statement There is a file named file.txtfile.txt with the following content:

file.txt
Hello World
This is a test file
file.txt
Hello World
This is a test file
with.py
# Open a file in read mode
with open("file.txt", "r") as file_object:
    # Read the file
    data = file_object.read()
    print(data)
 
# The file is automatically closed
with.py
# Open a file in read mode
with open("file.txt", "r") as file_object:
    # Read the file
    data = file_object.read()
    print(data)
 
# The file is automatically closed

Output:

command
C:\Users\username>python with.py
Hello World
This is a test file
command
C:\Users\username>python with.py
Hello World
This is a test file

In the above example, we open a file in read mode using the withwith statement. Then, we read the file using the read()read() method. Finally, we exit the withwith statement, and the file is automatically closed.

Reading in Binary Mode

We can read a file in binary mode using the read()read() method. This method reads a string from the file. The string can be a string literal or a variable that contains a string. We need to use rbrb as the mode while opening the file.

Syntax:

binary.py
file_object = open(file_name, "rb")
file_object.read([size])
binary.py
file_object = open(file_name, "rb")
file_object.read([size])

Here,

  • file_objectfile_object is the file object that we want to read.
  • sizesize is an optional numeric argument. When it is provided, it reads that many characters from the file. If the size parameter is not specified, it reads and returns up to the end of the file.
  • The read()read() method returns the specified number of bytes from the file. If we omit the size argument, it returns and displays all the data from the file.
  • The write()write() method writes a string to the file.

Example: Reading in Binary Mode There is a file named file.txtfile.txt with the following content:

file.txt
Hello World
file.txt
Hello World
binary.py
# Open a file in binary mode
file_object = open("file.txt", "rb")
 
# Read the file
data = file_object.read()
 
# Close the file
file_object.close()
 
# Print the data
print(data)
binary.py
# Open a file in binary mode
file_object = open("file.txt", "rb")
 
# Read the file
data = file_object.read()
 
# Close the file
file_object.close()
 
# Print the data
print(data)

Output:

command
C:\Users\username>python binary.py
b'Hello World\n'
command
C:\Users\username>python binary.py
b'Hello World\n'

In the above example, we open a file in binary mode using the open()open() function. Then, we read the file using the read()read() method. Finally, we close the file using the close()close() method.

Reading in Binary Mode Using the with Statement

We can also read a file in binary mode using the withwith statement. This statement creates a temporary variable (file_objectfile_object in the example below) that we can use to access the file inside the indented block of the withwith statement. When we exit the withwith statement, the file is automatically closed.

Example: Reading in Binary Mode Using the with Statement There is a file named file.txtfile.txt with the following content:

file.txt
Hello World
file.txt
Hello World
with.py
# Open a file in binary mode
with open("file.txt", "rb") as file_object:
    # Read the file
    data = file_object.read()
    print(data)
 
# The file is automatically closed
with.py
# Open a file in binary mode
with open("file.txt", "rb") as file_object:
    # Read the file
    data = file_object.read()
    print(data)
 
# The file is automatically closed

Output:

command
C:\Users\username>python with.py
b'Hello World\n'
command
C:\Users\username>python with.py
b'Hello World\n'

In the above example, we open a file in binary mode using the withwith statement. Then, we read the file using the read()read() method. Finally, we exit the withwith statement, and the file is automatically closed.

Reading a File Line by Line Using the with Statement

We can also read a file line by line using the withwith statement. This statement creates a temporary variable (file_objectfile_object in the example below) that we can use to access the file inside the indented block of the withwith statement. When we exit the withwith statement, the file is automatically closed.

Example: Reading a File Line by Line Using the with Statement There is a file named file.txtfile.txt with the following content:

file.txt
Hello World
This is a test file
file.txt
Hello World
This is a test file
with.py
# Open a file in read mode
with open("file.txt", "r") as file_object:
    # Read the file line by line
    line1 = file_object.readline()
    line2 = file_object.readline()
    print(line1)
    print(line2)
 
# The file is automatically closed
with.py
# Open a file in read mode
with open("file.txt", "r") as file_object:
    # Read the file line by line
    line1 = file_object.readline()
    line2 = file_object.readline()
    print(line1)
    print(line2)
 
# The file is automatically closed

Output:

command
C:\Users\username>python with.py
Hello World
This is a test file
command
C:\Users\username>python with.py
Hello World
This is a test file

In the above example, we open a file in read mode using the withwith statement. Then, we read the file line by line using the readline()readline() method. Finally, we exit the withwith statement, and the file is automatically closed.

Read Integer from a File

We can read an integer from a file using the read()read() method. In this example, we read an integer from a file using the read()read() method from bytes data.

Example: Read Integer from a File

write.py
# Open a file in write mode
file_object = open("file.txt", "wb")
 
# Write to the file
file_object.write(123.to_bytes(2, byteorder="big"))
 
# Close the file
file_object.close()
write.py
# Open a file in write mode
file_object = open("file.txt", "wb")
 
# Write to the file
file_object.write(123.to_bytes(2, byteorder="big"))
 
# Close the file
file_object.close()
read.py
# Open a file in read mode
file_object = open("file.txt", "rb")
 
# Read the file
data = file_object.read()
 
# Close the file
file_object.close()
 
# Print the data
print(int.from_bytes(data, byteorder="big"))
read.py
# Open a file in read mode
file_object = open("file.txt", "rb")
 
# Read the file
data = file_object.read()
 
# Close the file
file_object.close()
 
# Print the data
print(int.from_bytes(data, byteorder="big"))

Output:

command
C:\Users\username>python write.py
C:\Users\username>python read.py
123
command
C:\Users\username>python write.py
C:\Users\username>python read.py
123

In the above example, we open a file in write mode using the open()open() function. Then, we write the integer 123123 to the file using the write()write() method. Finally, we close the file using the close()close() method.

Read Float from a File

We can read a float from a file using the read()read() method. In this example, we read a float from a file using the read()read() method from bytes data.

Example: Read Float from a File

write.py
import struct
 
# Open a file in write mode
file_object = open("file.txt", "wb")
 
# Write to the file
file_object.write(struct.pack("f", 123.456))
 
# Close the file
file_object.close()
write.py
import struct
 
# Open a file in write mode
file_object = open("file.txt", "wb")
 
# Write to the file
file_object.write(struct.pack("f", 123.456))
 
# Close the file
file_object.close()
read.py
import struct
 
# Open a file in read mode
file_object = open("file.txt", "rb")
 
# Read the file
data = file_object.read()
 
# Close the file
file_object.close()
 
# Print the data
print(struct.unpack("f", data)[0])
read.py
import struct
 
# Open a file in read mode
file_object = open("file.txt", "rb")
 
# Read the file
data = file_object.read()
 
# Close the file
file_object.close()
 
# Print the data
print(struct.unpack("f", data)[0])

Output:

command
C:\Users\username>python write.py
C:\Users\username>python read.py
123.456
command
C:\Users\username>python write.py
C:\Users\username>python read.py
123.456

In the above example, we open a file in write mode using the open()open() function. Then, we write the float 123.456123.456 to the file using the write()write() method. Finally, we close the file using the close()close() method.

Using r+ mode to Read and Write to a File

We can use the r+r+ mode to read and write to a file. This mode opens the file for both reading and writing. It overwrites the existing file if the file exists. If the file does not exist, it creates a new file for reading and writing.

seek() Method

We can use the seek()seek() method to move the file pointer to a specific position in the file. This method accepts two arguments, the offset and the position. The position is optional and defaults to 00.

Syntax:

seek.py
file_object.seek(offset, [position])
seek.py
file_object.seek(offset, [position])

Here,

  • file_objectfile_object is the file object that we want to read.
  • offsetoffset is the number of bytes to be moved.
  • positionposition is the reference position from where the bytes are to be moved. It is optional and defaults to 00.
  • The seek()seek() method moves the file pointer to a specific position in the file.

Example: Using seek() Method There is a file named file.txtfile.txt with the following content:

file.txt
Hello World
This is a test file
file.txt
Hello World
This is a test file
seek.py
# Open a file in write mode
file_object = open("file.txt", "r+")
 
# Move the file pointer to the word "World"
file_object.seek(6)
 
# Read the file
data = file_object.read(5)
 
# Print the data
print(data)
 
# Close the file
file_object.close()
seek.py
# Open a file in write mode
file_object = open("file.txt", "r+")
 
# Move the file pointer to the word "World"
file_object.seek(6)
 
# Read the file
data = file_object.read(5)
 
# Print the data
print(data)
 
# Close the file
file_object.close()

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 in write mode using the open()open() function. Then, we move the file pointer to the word "World""World" using the seek()seek() method. Then, we read the next five characters using the read()read() method. Finally, we print the data.

Another example of using the seek()seek() method is to move the file pointer to different positions in the file.

Example: Using seek() Method There is a file named file.txtfile.txt with the following content:

file.txt
My Name is Ravi Kishan
I am a Software Engineer
I love to code
file.txt
My Name is Ravi Kishan
I am a Software Engineer
I love to code
seek.py
# Open a file in write mode
file_object = open("file.txt", "r+")
 
# Move the file pointer to get the name "Ravi Kishan"
file_object.seek(11)
 
# Read the file
data = file_object.read(11)
 
# Print the data
print(data)
 
# Move the file pointer to get the name "Software Engineer"
file_object.seek(34)
 
# Read the file
data = file_object.read(18)
 
# Print the data
print(data)
 
# Move the file pointer to get the name "code"
file_object.seek(55)
 
# Read the file
data = file_object.read(4)
 
# Print the data
print(data)
 
# Close the file
file_object.close()
seek.py
# Open a file in write mode
file_object = open("file.txt", "r+")
 
# Move the file pointer to get the name "Ravi Kishan"
file_object.seek(11)
 
# Read the file
data = file_object.read(11)
 
# Print the data
print(data)
 
# Move the file pointer to get the name "Software Engineer"
file_object.seek(34)
 
# Read the file
data = file_object.read(18)
 
# Print the data
print(data)
 
# Move the file pointer to get the name "code"
file_object.seek(55)
 
# Read the file
data = file_object.read(4)
 
# Print the data
print(data)
 
# Close the file
file_object.close()

Output:

command
C:\Users\username>python seek.py
Ravi Kishan
Software Engineer
code
command
C:\Users\username>python seek.py
Ravi Kishan
Software Engineer
code

In the above example, we open a file in write mode using the open()open() function. Then, we move the file pointer to get the name "Ravi Kishan""Ravi Kishan" using the seek()seek() method. Then, we read the next eleven characters using the read()read() method. Finally, we print the data. Next, we move the file pointer to get the name "Software Engineer""Software Engineer" using the seek()seek() method. Then, we read the next eighteen characters using the read()read() method. Finally, we print the data. Next, we move the file pointer to get the name "code""code" using the seek()seek() method. Then, we read the next four characters using the read()read() method. Finally, we print the data.

Reading and Writing to a File Simultaneously

We can read and write to a file simultaneously using the r+r+ mode. This mode opens the file for both reading and writing. It overwrites the existing file if the file exists. If the file does not exist, it creates a new file for reading and writing.

read_write.py
# Open a file in write mode
file_object = open("file.txt", "r+")
 
# Write to the file
file_object.write("Hello World")
 
# Move the file pointer to the beginning of the file
file_object.seek(0)
 
# Read the file
data = file_object.read()
 
# Print the data
print(data)
 
# Close the file
file_object.close()
read_write.py
# Open a file in write mode
file_object = open("file.txt", "r+")
 
# Write to the file
file_object.write("Hello World")
 
# Move the file pointer to the beginning of the file
file_object.seek(0)
 
# Read the file
data = file_object.read()
 
# Print the data
print(data)
 
# Close the file
file_object.close()

Output:

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

In the above example, we open a file in write mode using the open()open() function. Then, we write the string "Hello World""Hello World" to the file using the write()write() method. Next, we move the file pointer to the beginning of the file using the seek()seek() method. Then, we read the file using the read()read() method. Finally, we print the data.

Reading and Writing to a File Using the with Statement

We can also read and write to a file using the withwith statement. This statement creates a temporary variable (file_objectfile_object in the example below) that we can use to access the file inside the indented block of the withwith statement. When we exit the withwith statement, the file is automatically closed.

Example: Reading and Writing to a File Using the with Statement

with.py
# Open a file in read mode
with open("file.txt", "r+") as file_object:
    # Write to the file
    file_object.write("Hello World")
 
    # Move the file pointer to the beginning of the file
    file_object.seek(0)
 
    # Read the file
    data = file_object.read()
 
    # Print the data
    print(data)
 
# The file is automatically closed
with.py
# Open a file in read mode
with open("file.txt", "r+") as file_object:
    # Write to the file
    file_object.write("Hello World")
 
    # Move the file pointer to the beginning of the file
    file_object.seek(0)
 
    # Read the file
    data = file_object.read()
 
    # Print the data
    print(data)
 
# The file is automatically closed

Output:

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

In the above example, we open a file in read mode using the withwith statement. Then, we write the string "Hello World""Hello World" to the file using the write()write() method. Next, we move the file pointer to the beginning of the file using the seek()seek() method. Then, we read the file using the read()read() method. Finally, we print the data. Next, we exit the withwith statement, and the file is automatically closed.

Conclusion

In python, we can read and write to a file using the open()open() function. This function accepts two arguments, the name of the file and the mode in which we want to open the file. The mode can be read mode, write mode, append mode, etc. The default mode is read mode. We can also use the withwith statement to read and write to a file. This statement creates a temporary variable (file_objectfile_object in the example below) that we can use to access the file inside the indented block of the withwith statement. When we exit the withwith statement, the file is automatically closed. For more information, check out the official documentation. For more tutorials, check out the Python Central Hub.

Was this page helpful?

Let us know how we did