This tutorial will teach you how to work with files in Python. Files are commonly used for permanent data storage. Python features a useful function for reading and writing data to and from files. It can perform operations such as creating, reading, updating, and deleting files, among other things.
Why Do We Need File Handling
The files are used as repositories for permanent data storage within applications. You can read, update, and delete data stored in various common file formats, such as text, CSV, Excel, and JSON.
Python has built-in methods for reading, writing, and deleting files. The open()
function is used to read and write files. For reading, writing, appending and creating files, the function open()
requires two parameters: filename and mode ('r'
, 'w'
, 'a'
, 'x'
).
You can also check out other python tutorials:
- How To Create and Write JSON file in Python
- How To Load Json file Using Python
- Python List Example And Methods
- Convert Python Dictionary To JSON
Creating a File in Python
To create a new file, we can use the open()
function with the file name and mode x
. If the file does not exist, it will be created; otherwise, an error will be returned.
file = open("test.txt", "x")
The above code will create a test.txt
file.
Writing a File in Python
The write()
a function can be used to save data to a file. The write()
function overwrites the contents of a file. To write to a file, we must open it in write mode w
.
file = open("test.txt", "w") file.write("Content to write into test.txt file.") file.close()
The preceding code will open the file test.txt
in write mode and use the write()
method to write content to it.
Appending Content into a File
By opening a file in append a mode, We can also append material to it.
file = open("test.txt", "a") file.write("Content to append into test.txt file.") file.close()
Appending Content into a File
By opening a file in append a mode, we can also append material to it.
file = open("test.txt", "a") file.write("Content to append into test.txt file.") file.close()
Reading files in Python
We can read the contents of a file by opening it in reading mode. After opening the file in reading mode, we must use the read()
function to read the contents.
file = open("test.txt", "r") print(file.read()) file.close()
The code above will open the file in read-only mode and then use the read()
function to read the contents.
How to read file Line By Line
We can also go line by line through the entire file. The file must be read and looped through. Here’s how to read a file line by line using code.
file = open("test.txt", "r") for x in file: print(x) file.close()
Closing a file in Python
When you are finished with a file, it is always a good idea to close it. To close the file, use the close() function, as shown below.
file = open("test.txt", "r") print(file.read()) file.close()
We are closing the file using file.close()
after the file reading is done.
Deleting a file in Python
The remove()
a function is used to delete the file, we must first import the OS
module.
import os if os.path.exists("test.txt"): os.remove("test.txt") else: print("The file does not exist")
The above code will import the OS
module and check for the existence of the file test.txt
before deleting it. If the file already exists, use the remove()
function to destroy it; otherwise, a message stating that the file does not exist will be displayed.
Conclusion
I have compiled some common file handling methods for Python. These are built-in file methods in Python that allow you to open, read, write, update, and delete files.