Python 3 – File Methods
Files are used as a way to manage and store data permanently on a computer. In programming, we use file methods to create, open, read, write and delete files. In this article, we will discuss the Python file methods and their functionalities.
Opening a File
Before we can read or write data to a file in Python, we must first open the file. We can use the open()
function to open a file. The syntax to open a file in Python is as follows:
file_object = open(file_name [, access_mode][, buffering])
where:
file_name
: Name of the file to be opened or created.access_mode
: The mode in which the file has to be opened. It can be'r'
(read mode),'w'
(write mode),'a'
(append mode) or'x'
(exclusive creation mode).buffering
: An optional integer used for buffering the data. A value of0
(default) means no buffering, and a value of1
means line buffering.
Example
Let’s look at how we can open a file for reading and writing:
# Open a file in read mode
file_obj = open('myfile.txt', 'r')
# Open a file in write mode
file_obj = open('myfile.txt', 'w')
Closing a File
After opening a file, it is essential to close it once the operations are done. It frees up any system resources that were used by the file. We can use the close()
method to close a file. The syntax to close a file is as follows:
file_object.close()
Example
Here’s how we can close a file:
# Open a file
file_obj = open('myfile.txt', 'r')
# Do some operations on the file
# ...
# Close the file
file_obj.close()
Reading a File
Once the file is open in read mode, we can read data from the file. We can use the read()
method to read the entire file contents or the readline()
method to read one line at a time. The syntax to read a file is as follows:
file_object.read(size)
where size
is the number of bytes to be read.
file_object.readline(size)
where size
is the number of bytes to be read. If size
is not specified, then it will read one line at a time.
Example
Let’s look at an example of reading a file using the read()
method:
# Open a file in read mode
file_obj = open('myfile.txt', 'r')
# Read the file contents
file_contents = file_obj.read()
# Close the file
file_obj.close()
print(file_contents)
The above code will read the entire contents of the file and print it.
Writing to a File
In order to write data to a file, we need to open it in the write mode using the open()
function. We can then use the write()
method to write data to the file. The syntax to write to a file is as follows:
file_object.write(string)
where string
is the data to be written to the file.
Example
Let’s look at an example of writing to a file:
# Open a file in write mode
file_obj = open('myfile.txt', 'w')
# Write some data to the file
file_obj.write('Hello, world!')
# Close the file
file_obj.close()
This code will create a new file if it doesn’t exist, and write the text 'Hello, world!'
to the file.
Deleting a File
We can use the os
module to delete a file in Python. We can use the remove()
method to delete a file. The syntax to delete a file is as follows:
os.remove(file_name)
where file_name
is the name of the file to be deleted.
Example
Let’s look at an example of deleting a file:
import os
# Delete a file
os.remove('myfile.txt')
Conclusion
In this article, we have discussed how to use Python file methods to create, open, read, write and delete files. We hope this article has helped you understand the basics of Python file handling. With the knowledge gained, you can now move forward and create your own Python applications that require file I/O.