Python 3 – Files I/O
One of the important features of any programming language is the ability to work with files. In Python 3, there are several ways to read and write files. In this article, we will discuss how to open, read, write and close files in Python 3.
Opening a File
To open a file in Python 3, we use the open()
function. The syntax for the open()
function is as follows:
file_object = open(file_name [, access_mode][, buffering])
Here, file_name
is a string that specifies the name and path of the file that we want to open. The access_mode
parameter is optional, and it specifies whether we want to read, write or append to the file. The buffering
parameter is also optional, and it specifies the size of the buffer used in read and write operations.
The following table shows the various access modes that can be used with the open()
function:
Mode | Description |
---|---|
‘r’ | Open for reading (default) |
‘w’ | Open for writing, truncating the file first |
‘x’ | Open for exclusive creation, failing if the file already exists |
‘a’ | Open for writing, appending to the end of the file if it exists |
‘b’ | Binary mode |
‘t’ | Text mode (default) |
‘+’ | Open for updating (reading and writing) |
Here is an example that shows how to open a file in text mode for reading:
# Open the file for reading
file = open("example.txt", "r")
# Do something with the file
# Close the file
file.close()
In this example, we open the file “example.txt” in read mode by specifying the access mode as “r”. We then perform some operation on the file before finally closing it using the close()
method.
Reading from a File
Once we have opened a file in Python 3, we can read its contents using the read()
method. The read()
method reads the entire contents of the file and returns it as a string. Here is an example that demonstrates the use of the read()
method:
# Open the file for reading
file = open("example.txt", "r")
# Read the entire file contents
content = file.read()
# Print the contents
print(content)
# Close the file
file.close()
In this example, we open the file “example.txt” in read mode and read its entire contents using the read()
method. We then print the contents to the console and close the file using the close()
method.
Sometimes, we may want to read the contents of a file line by line. We can do this using the readline()
method. The readline()
method reads a single line from the file and returns it as a string. Here is an example that demonstrates the use of the readline()
method:
# Open the file for reading
file = open("example.txt", "r")
# Read the first line
line1 = file.readline()
# Read the second line
line2 = file.readline()
# Print the lines
print(line1)
print(line2)
# Close the file
file.close()
In this example, we open the file “example.txt” in read mode and read the first two lines using the readline()
method. We then print the lines to the console and close the file using the close()
method.
Writing to a File
To write to a file in Python 3, we use the write()
method. The write()
method writes a string to the file. If the file does not exist, it is created. Here is an example that demonstrates the use of the write()
method:
# Open the file for writing
file = open("example.txt", "w")
# Write some text to the file
file.write("Hello, world!\n")
# Close the file
file.close()
In this example, we open the file “example.txt” in write mode and write the string “Hello, world!\n” to it using the write()
method. We then close the file using the close()
method.
Closing a File
Once we have finished working with a file in Python 3, it is important to close it using the close()
method. This frees up any system resources that were being used by the file. If we do not close the file, we may run into issues such as memory leaks or problems with other programs accessing the file. Here is an example that shows how to close a file:
# Open the file
file = open("example.txt", "r")
# Do something with the file
# Close the file
file.close()
In this example, we open the file “example.txt” and perform some operation on it before finally closing it using the close()
method.
Conclusion
In this article, we have seen how to open, read, write and close files in Python 3. We have also seen the various access modes that can be used with the open()
function, as well as the read()
, readline()
and write()
methods. It is important to always close files once we have finished working with them to avoid any issues with system resources. With these concepts in mind, we can effectively work with files in our Python 3 programs.