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:
file_object = open(file_name, [access_mode], [buffering])
file_object = open(file_name, [access_mode], [buffering])
Here,
file_name
file_name
is the name of the file that we want to access.access_mode
access_mode
is the mode in which we want to open the file, i.e., read, write, append, etc.buffering
buffering
is an optional integer used to set the buffering policy.file_object
file_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. | Mode | Description |
---|---|---|
1 | w w | Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing. |
2 | wb wb | Opens 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. |
3 | w+ 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. |
4 | wb+ 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. |
5 | a a | Opens 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. |
6 | ab ab | Opens 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. |
7 | a+ 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. |
8 | ab+ 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:
file_object.write(string)
file_object.write(string)
Here, string
string
is the string data that we want to write to the file.
Example: Writing to a File
# 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()
# 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:
C:\Users\username>python write.py
C:\Users\username>python write.py
Hello World
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
# 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()
# 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:
C:\Users\username>python write.py
C:\Users\username>python write.py
Hello World
This is a test file
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
# 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()
# 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:
C:\Users\username>python write.py
C:\Users\username>python write.py
['Hello', 'World', 'This', 'is', 'a', 'test', 'file']
['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 with
with
statement. This statement creates a temporary variable (file_object
file_object
in the example below) that we can use to access the file inside the indented block of the with
with
statement. When we exit the with
with
statement, the file is automatically closed.
Example: Writing to a File Using the with Statement
# 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
# 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:
C:\Users\username>python with.py
C:\Users\username>python with.py
Hello World
Hello World
In the above example, we open a file in write mode using the with
with
statement. Then, we write the string "Hello World"
"Hello World"
to the file using the write()
write()
method. Finally, we exit the with
with
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 a
a
as the mode while opening the file.
Syntax:
file_object = open(file_name, "a")
file_object.write(string)
file_object = open(file_name, "a")
file_object.write(string)
Here,
file_name
file_name
is the name of the file that we want to access.string
string
is the string data that we want to write to the file.file_object
file_object
is the object that we can use to access the file.a
a
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.txt
file.txt
with the following content:
Hello World
Hello World
# 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()
# 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:
C:\Users\username>python append.py
C:\Users\username>python append.py
Hello World
This is a test file
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.txt
file.txt
with the following content:
This is a head
This is a head
# 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()
# 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:
C:\Users\username>python append.py
C:\Users\username>python append.py
This is a head
Hello World
This is a test file
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 wb
wb
as the mode while opening the file.
Syntax:
file_object = open(file_name, "wb")
file_object.write(string)
file_object = open(file_name, "wb")
file_object.write(string)
Here,
file_name
file_name
is the name of the file that we want to access.string
string
is the string data that we want to write to the file.file_object
file_object
is the object that we can use to access the file.wb
wb
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
# 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()
# 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:
C:\Users\username>python binary.py
C:\Users\username>python binary.py
Hello World
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 0
0
.
Syntax:
file_object.seek(offset, [position])
file_object.seek(offset, [position])
Here,
file_object
file_object
is the file object that we want to read.offset
offset
is the number of bytes to be moved.position
position
is the reference position from where the bytes are to be moved. It is optional and defaults to0
0
.- 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
# 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())
# 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:
C:\Users\username>python seek.py
World
11
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
# 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)
# 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:
C:\Users\username>python seek.py
C:\Users\username>python seek.py
Hello Python
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:
file_object.read([size])
file_object.read([size])
Here,
file_object
file_object
is the file object that we want to read.size
size
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:
file_object = open(file_name, [access_mode], [buffering])
file_object = open(file_name, [access_mode], [buffering])
Here,
file_name
file_name
is the name of the file that we want to access.access_mode
access_mode
is the mode in which we want to open the file, i.e., read, write, append, etc.buffering
buffering
is an optional integer used to set the buffering policy.file_object
file_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. | Mode | Description |
---|---|---|
1 | r r | Opens a file in read-only mode. The file pointer is placed at the beginning of the file. This is the default mode. |
2 | rb rb | Opens 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. |
3 | r+ r+ | Opens a file for both reading and writing. The file pointer placed at the beginning of the file. |
4 | rb+ 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.txt
file.txt
with the following content:
Hello World
This is a test file
Hello World
This is a test file
# 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)
# 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:
C:\Users\username>python read.py
Hello World
This is a test file
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:
file_object.readline()
file_object.readline()
Here,
file_object
file_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.txt
file.txt
with the following content:
Hello World
This is a test file
Hello World
This is a test file
# 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)
# 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:
C:\Users\username>python readline.py
Hello World
This is a test file
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.txt
file.txt
with the following content:
Hello World
This is a test file
Hello World
This is a test file
# 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)
# 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:
C:\Users\username>python readlines.py
['Hello World\n', 'This is a test file\n']
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 with
with
statement. This statement creates a temporary variable (file_object
file_object
in the example below) that we can use to access the file inside the indented block of the with
with
statement. When we exit the with
with
statement, the file is automatically closed.
Example: Reading a File Using the with Statement
There is a file named file.txt
file.txt
with the following content:
Hello World
This is a test file
Hello World
This is a test file
# 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
# 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:
C:\Users\username>python with.py
Hello World
This is a test file
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 with
with
statement. Then, we read the file using the read()
read()
method. Finally, we exit the with
with
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 rb
rb
as the mode while opening the file.
Syntax:
file_object = open(file_name, "rb")
file_object.read([size])
file_object = open(file_name, "rb")
file_object.read([size])
Here,
file_object
file_object
is the file object that we want to read.size
size
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.txt
file.txt
with the following content:
Hello World
Hello World
# 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)
# 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:
C:\Users\username>python binary.py
b'Hello World\n'
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 with
with
statement. This statement creates a temporary variable (file_object
file_object
in the example below) that we can use to access the file inside the indented block of the with
with
statement. When we exit the with
with
statement, the file is automatically closed.
Example: Reading in Binary Mode Using the with Statement
There is a file named file.txt
file.txt
with the following content:
Hello World
Hello World
# 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
# 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:
C:\Users\username>python with.py
b'Hello World\n'
C:\Users\username>python with.py
b'Hello World\n'
In the above example, we open a file in binary mode using the with
with
statement. Then, we read the file using the read()
read()
method. Finally, we exit the with
with
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 with
with
statement. This statement creates a temporary variable (file_object
file_object
in the example below) that we can use to access the file inside the indented block of the with
with
statement. When we exit the with
with
statement, the file is automatically closed.
Example: Reading a File Line by Line Using the with Statement
There is a file named file.txt
file.txt
with the following content:
Hello World
This is a test file
Hello World
This is a test file
# 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
# 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:
C:\Users\username>python with.py
Hello World
This is a test file
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 with
with
statement. Then, we read the file line by line using the readline()
readline()
method. Finally, we exit the with
with
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
# 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()
# 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()
# 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"))
# 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:
C:\Users\username>python write.py
C:\Users\username>python read.py
123
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 123
123
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
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()
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()
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])
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:
C:\Users\username>python write.py
C:\Users\username>python read.py
123.456
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.456
123.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 0
0
.
Syntax:
file_object.seek(offset, [position])
file_object.seek(offset, [position])
Here,
file_object
file_object
is the file object that we want to read.offset
offset
is the number of bytes to be moved.position
position
is the reference position from where the bytes are to be moved. It is optional and defaults to0
0
.- 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.txt
file.txt
with the following content:
Hello World
This is a test file
Hello World
This is a test file
# 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()
# 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:
C:\Users\username>python seek.py
World
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.txt
file.txt
with the following content:
My Name is Ravi Kishan
I am a Software Engineer
I love to code
My Name is Ravi Kishan
I am a Software Engineer
I love to code
# 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()
# 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:
C:\Users\username>python seek.py
Ravi Kishan
Software Engineer
code
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.
# 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()
# 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:
C:\Users\username>python read_write.py
Hello World
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 with
with
statement. This statement creates a temporary variable (file_object
file_object
in the example below) that we can use to access the file inside the indented block of the with
with
statement. When we exit the with
with
statement, the file is automatically closed.
Example: Reading and Writing to a File Using the with Statement
# 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
# 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:
C:\Users\username>python with.py
Hello World
C:\Users\username>python with.py
Hello World
In the above example, we open a file in read mode using the with
with
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 with
with
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 with
with
statement to read and write to a file. This statement creates a temporary variable (file_object
file_object
in the example below) that we can use to access the file inside the indented block of the with
with
statement. When we exit the with
with
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