Write and Read File
Writing Files in Python
Section titled “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
Section titled “Opening a File in Write Mode”We can open a file in write mode using the 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])Here,
file_nameis the name of the file that we want to access.access_modeis the mode in which we want to open the file, i.e., read, write, append, etc.bufferingis an optional integer used to set the buffering policy.file_objectis the object that we can use to access the file.
File Opening Write Modes
Section titled “File Opening Write Modes”There are different modes in which we can open a file. These modes are:
| S.No. | Mode | Description |
|---|---|---|
| 1 | 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 | 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+ | 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+ | 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 | 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 | 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+ | 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+ | 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
Section titled “Writing to a File in Python”We can write to a file in Python using the 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)Here, 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()Output:
C:\Users\username>python write.pyHello WorldIn the above example, we open a file in write mode using the open() function. Then, we write the string "Hello World" to the file using the write() method. Finally, we close the file using the close() method.
Writing Multiple Lines to a File
Section titled “Writing Multiple Lines to a File”We can write multiple lines to a file using the write() method. To do so, we need to add a newline character (\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()Output:
C:\Users\username>python write.pyHello World
This is a test fileIn the above example, we write two lines to the file. We add a newline character (\n) at the end of each line to write the lines to the file.
Writing a List to a File
Section titled “Writing a List to a File”We can write a list to a file using the write() method. To do so, we need to convert the list to a string using the 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()Output:
C:\Users\username>python write.py['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() function before writing it to the file.
Writing to a File Using the with Statement
Section titled “Writing to a File Using the with Statement”We can also write to a file using the with statement. This statement creates a temporary variable (file_object in the example below) that we can use to access the file inside the indented block of the with statement. When we exit the 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 closedOutput:
C:\Users\username>python with.pyHello WorldIn the above example, we open a file in write mode using the with statement. Then, we write the string "Hello World" to the file using the write() method. Finally, we exit the with statement, and the file is automatically closed.
Appending to a File in Python
Section titled “Appending to a File in Python”We can append to a file in Python using the 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 as the mode while opening the file.
Syntax:
file_object = open(file_name, "a")
file_object.write(string)Here,
file_nameis the name of the file that we want to access.stringis the string data that we want to write to the file.file_objectis the object that we can use to access the file.ais 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 with the following content:
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()Output:
C:\Users\username>python append.pyHello World
This is a test fileIn the above example, we open a file in append mode using the open() function. Then, we write the string "Hello World" to the file using the write() method. Finally, we close the file using the close() method.
Appending Multiple Lines to a File
Section titled “Appending Multiple Lines to a File”We can append multiple lines to a file using the write() method. To do so, we need to add a newline character (\n) at the end of each line.
Example: Appending Multiple Lines to a File
There is a file named file.txt with the following content:
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()Output:
C:\Users\username>python append.pyThis is a head
Hello World
This is a test fileIn the above example, we write two lines to the file. We add a newline character (\n) at the end of each line to write the lines to the file.
Writing in Binary Mode
Section titled “Writing in Binary Mode”We can write to a file in binary mode using the 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 as the mode while opening the file.
Syntax:
file_object = open(file_name, "wb")
file_object.write(string)Here,
file_nameis the name of the file that we want to access.stringis the string data that we want to write to the file.file_objectis the object that we can use to access the file.wbis the mode in which we want to open the file, i.e., binary mode.- The
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()Output:
C:\Users\username>python binary.pyHello WorldIn the above example, we open a file in binary mode using the open() function. Then, we write the string "Hello World" to the file using the write() method. Finally, we close the file using the close() method.
Using w+ Mode to Write to a File
Section titled “Using w+ Mode to Write to a File”We can use the 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
Section titled “seek() Method”We can use the 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.
Syntax:
file_object.seek(offset, [position])Here,
file_objectis the file object that we want to read.offsetis the number of bytes to be moved.positionis the reference position from where the bytes are to be moved. It is optional and defaults to0.- The
seek()method moves the file pointer to a specific position in the file. - The
tell()method returns the current position of the file pointer. - The
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())Output:
C:\Users\username>python seek.py
World
11In the above example, we open a file in write mode using the open() function. Then, we write the string "Hello World" to the file using the write() method. Next, we move the file pointer to the word "World" using the seek() method. Then, we read the next five characters using the read() method. Finally, we print the data and the current position of the file pointer using the 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)Output:
C:\Users\username>python seek.pyHello PythonIn the above example, we open a file in write mode using the open() function. Then, we write the string "Hello World" to the file using the write() method. Next, we move the file pointer to the word "World" using the seek() method. Then, we read the next five characters using the read() method. Next, we move the file pointer to the word "World" using the seek() method. Then, we write the string "Python" to the file using the write() method. Finally, we move the file pointer to the beginning of the file using the seek() method.
Reading Files in Python
Section titled “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])Here,
file_objectis the file object that we want to read.sizeis 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()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
Section titled “Opening a File in Write Mode”We can open a file in write mode using the 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])Here,
file_nameis the name of the file that we want to access.access_modeis the mode in which we want to open the file, i.e., read, write, append, etc.bufferingis an optional integer used to set the buffering policy.file_objectis the object that we can use to access the file.
File Opening Modes
Section titled “File Opening Modes”There are different modes in which we can open a file. These modes are:
| S.No. | Mode | Description |
|---|---|---|
| 1 | 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 | 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+ | Opens a file for both reading and writing. The file pointer placed at the beginning of the file. |
| 4 | 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 with the following content:
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)Output:
C:\Users\username>python read.py
Hello World
This is a test fileIn the above example, we open a file in read mode using the open() function. Then, we read the file using the read() method. Finally, we close the file using the close() method.
Reading a File Line by Line
Section titled “Reading a File Line by Line”We can read a file line by line using the 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()Here,
file_objectis the file object that we want to read.- The
readline()method returns the next line from the file. - The
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 with the following content:
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)Output:
C:\Users\username>python readline.py
Hello World
This is a test fileIn the above example, we open a file in read mode using the open() function. Then, we read the file line by line using the readline() method. Finally, we close the file using the close() method.
Example: Reading a File Line by Line using readlines() Method
There is a file named file.txt with the following content:
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)Output:
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() function. Then, we read the file line by line using the readlines() method. Finally, we close the file using the close() method.
Reading a File Using the with Statement
Section titled “Reading a File Using the with Statement”We can also read a file using the with statement. This statement creates a temporary variable (file_object in the example below) that we can use to access the file inside the indented block of the with statement. When we exit the with statement, the file is automatically closed.
Example: Reading a File Using the with Statement
There is a file named file.txt with the following content:
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 closedOutput:
C:\Users\username>python with.py
Hello World
This is a test fileIn the above example, we open a file in read mode using the with statement. Then, we read the file using the read() method. Finally, we exit the with statement, and the file is automatically closed.
Reading in Binary Mode
Section titled “Reading in Binary Mode”We can read a file in binary mode using the 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 as the mode while opening the file.
Syntax:
file_object = open(file_name, "rb")
file_object.read([size])Here,
file_objectis the file object that we want to read.sizeis 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()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()method writes a string to the file.
Example: Reading in Binary Mode
There is a file named file.txt with the following content:
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)Output:
C:\Users\username>python binary.py
b'Hello World\n'In the above example, we open a file in binary mode using the open() function. Then, we read the file using the read() method. Finally, we close the file using the close() method.
Reading in Binary Mode Using the with Statement
Section titled “Reading in Binary Mode Using the with Statement”We can also read a file in binary mode using the with statement. This statement creates a temporary variable (file_object in the example below) that we can use to access the file inside the indented block of the with statement. When we exit the with statement, the file is automatically closed.
Example: Reading in Binary Mode Using the with Statement
There is a file named file.txt with the following content:
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 closedOutput:
C:\Users\username>python with.py
b'Hello World\n'In the above example, we open a file in binary mode using the with statement. Then, we read the file using the read() method. Finally, we exit the with statement, and the file is automatically closed.
Reading a File Line by Line Using the with Statement
Section titled “Reading a File Line by Line Using the with Statement”We can also read a file line by line using the with statement. This statement creates a temporary variable (file_object in the example below) that we can use to access the file inside the indented block of the with statement. When we exit the 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 with the following content:
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 closedOutput:
C:\Users\username>python with.py
Hello World
This is a test fileIn the above example, we open a file in read mode using the with statement. Then, we read the file line by line using the readline() method. Finally, we exit the with statement, and the file is automatically closed.
Read Integer from a File
Section titled “Read Integer from a File”We can read an integer from a file using the read() method. In this example, we read an integer from a file using the 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 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
123In the above example, we open a file in write mode using the open() function. Then, we write the integer 123 to the file using the write() method. Finally, we close the file using the close() method.
Read Float from a File
Section titled “Read Float from a File”We can read a float from a file using the read() method. In this example, we read a float from a file using the 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 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.456In the above example, we open a file in write mode using the open() function. Then, we write the float 123.456 to the file using the write() method. Finally, we close the file using the close() method.
Using r+ mode to Read and Write to a File
Section titled “Using r+ mode to Read and Write to a File”We can use the 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
Section titled “seek() Method”We can use the 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.
Syntax:
file_object.seek(offset, [position])Here,
file_objectis the file object that we want to read.offsetis the number of bytes to be moved.positionis the reference position from where the bytes are to be moved. It is optional and defaults to0.- The
seek()method moves the file pointer to a specific position in the file.
Example: Using seek() Method
There is a file named file.txt with the following content:
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()Output:
C:\Users\username>python seek.py
WorldIn the above example, we open a file in write mode using the open() function. Then, we move the file pointer to the word "World" using the seek() method. Then, we read the next five characters using the read() method. Finally, we print the data.
Another example of using the 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 with the following content:
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()Output:
C:\Users\username>python seek.py
Ravi Kishan
Software Engineer
codeIn the above example, we open a file in write mode using the open() function. Then, we move the file pointer to get the name "Ravi Kishan" using the seek() method. Then, we read the next eleven characters using the read() method. Finally, we print the data. Next, we move the file pointer to get the name "Software Engineer" using the seek() method. Then, we read the next eighteen characters using the read() method. Finally, we print the data. Next, we move the file pointer to get the name "code" using the seek() method. Then, we read the next four characters using the read() method. Finally, we print the data.
Reading and Writing to a File Simultaneously
Section titled “Reading and Writing to a File Simultaneously”We can read and write to a file simultaneously using the 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()Output:
C:\Users\username>python read_write.py
Hello WorldIn the above example, we open a file in write mode using the open() function. Then, we write the string "Hello World" to the file using the write() method. Next, we move the file pointer to the beginning of the file using the seek() method. Then, we read the file using the read() method. Finally, we print the data.
Reading and Writing to a File Using the with Statement
Section titled “Reading and Writing to a File Using the with Statement”We can also read and write to a file using the with statement. This statement creates a temporary variable (file_object in the example below) that we can use to access the file inside the indented block of the with statement. When we exit the 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 closedOutput:
C:\Users\username>python with.py
Hello WorldIn the above example, we open a file in read mode using the with statement. Then, we write the string "Hello World" to the file using the write() method. Next, we move the file pointer to the beginning of the file using the seek() method. Then, we read the file using the read() method. Finally, we print the data. Next, we exit the with statement, and the file is automatically closed.
Conclusion
Section titled “Conclusion”In python, we can read and write to a file using the 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 statement to read and write to a file. This statement creates a temporary variable (file_object in the example below) that we can use to access the file inside the indented block of the with statement. When we exit the with statement, the file is automatically closed. For more information, check out the official documentation. For more tutorials, check out the Python Central Hub.
Try it: Write and Read File Exercises
Section titled “Try it: Write and Read File Exercises”Exercise 1 – Write and Read Text
Section titled “Exercise 1 – Write and Read Text”Exercise 2 – Loop and Read Lines
Section titled “Exercise 2 – Loop and Read Lines”Exercise 3 – Count Words in File
Section titled “Exercise 3 – Count Words in File”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading