Introduction
In most programs, we store data temporarily in the code editor, but the data is lost as soon as we exit the program. Let's consider data stored in a list, for example. Once we close the program, all the data in the List is lost.
With file io, we can read from, that is, load data from a file or write to, that is, save data into a file, ensuring that data persists even after ending the program.
The advantages of file I/O
- Simple and easy to understand hence, it can be used by both experienced and junior developer
- It supports various file formats therefore. It provides flexibility in working with different file formats.
- Python provides many built-in functions for file I/O, hence reducing the need for using external libraries, which are not reliable sometimes.
- Python file I/O provides functionality for error handling.
Disadvantages of file I/O
- It does not provide perform well for large files compared to low-level languages like C and C#.
- It consumes memory space, which can lead to memory issues in memory-constrained environments.
- Python file I/O has limited concurrency hence, it is less suitable for highly parallel tasks.
Practical uses of file I/O in Python
File I/O is fundamental for storing and retrieving data in applications and databases. It's used in automating tasks such as report generation and data transformation in big data.
Additionally, it's used when handling downloads and uploads in web development.
File modes
File modes are used to perform different tasks, such as reading and appending files. Here are some of the common modes:
-
'+'
: Update mode.Used to open a file for both reading and writing. -
'a'
: Append mode. Opens the file for appending. -
'b'
: Binary mode. Used when dealing with files in the binary mode. -
'r'
: Read mode. Opens the file for reading.
- 'w'
: Writing mode. Opens the file for writing.
Opening and writing files
The open
function in Python is used to open a file in Python for reading or writing. Let's write a program that opens a file, allows the user to enter data, and finally saves.
cars= input("what is the model of your car?")
file= open('cars.txt','w')
file.write(f'{cars}\n')
file.close()
In the code above, we are declaring a variable called cars used to store the input of the car model. We then open a file called cars.txt with the writing mode. Finally, we save the file by closing. The code above works perfectly, but the problem is that even though we write as many car models as we want, we will get only the last value. This is because the write mode overwrites the previous entry.
Now, let's solve the problem by writing a program that uses the append file mode.
cars= input("what is the model of your car?")
file= open('cars.txt','a')
file.write(f'{cars}\n')
file.close()
Let's add three models of cars, that is, Mazda, Honda, and Subaru, and see the output.
mazda
honda
subaru
The cars.txt file looks like the one above. Hence, our problem is solved.
Reading from files
Now let's see how we can read from files:
with open("cars.txt", "r") as file:
lines = file.readlines()
for line in lines:
print(f"My car is a, {line.rstrip()}")
The readlines
method reads all the lines from the file and stores them in a list, while the rstrip
method removes extra newline characters.
The output looks like the one below:
My car is a, mazda
My car is a, honda
My car is a, subaru
We can further simplify the code by reading line by line using rstrip
without readlines
.
with open("cars.txt", "r") as file:
for line in file:
print(f"My car is a, {line.rstrip()}")
The output from the terminal is the same as the previous one.
Let's move on and sort the names of the cars before printing.
cars = []
with open("cars.txt", "r") as file:
for line in file:
cars.append(line.rstrip())
for car in sorted(cars):
print(f"My car is a, {car}")
The output looks like the one below:
My car is a, honda
My car is a, mazda
My car is a, subaru
As evident from the output, the cars have been sorted using the first letter.
Working with CSV Files
Comma-separated values (CSV) files are commonly used in big data and storing tabular data. Let's create a simple CSV file named students.csv
as follows.
George, Kilimanjaro
Ronald,Elgon
Frank,Kilimanjaro
Washington,Elgon
We can open the file and read it using Python as follows.
students = []
with open("students.csv", "r") as file:
for line in file:
name, house = line.rstrip().split(",")
students.append({"name": name, "house": house})
for student in sorted(students, key=lambda student: student["name"]):
print(f"{student['name']} is in {student['house']}")
The code above reads the file, stores each data in a dictionary, and then sorts and prints students by name.
For more complex CSV
data, the CSV Python module is used. It can process CSV data with commas inside the data.
Writing to CSV FIles
Let's write data to a CSV file using the CSV
module as shown below.
import csv
name = input("What's your name? ")
house = input("What's your house? ")
with open("students.csv", "a") as file:
writer = csv.DictWriter(file, fieldnames=["name", "house"])
writer.writerow({"name": name, "house": house})
The code above prompts the user to enter a name and a house and then write to it using the append method.
Conclusion
File I/O is very important as it allows users to read and write data into files. In this article, we have discussed what file I/O is and its advantages and disadvantages. We also discussed the application of file I/O in the real world. We then looked into file modes, opening and writing files, and then working with CSV files. You can check out my GitHub link for more here.
Top comments (0)