File Handling#
We usually store our data in different file formats. In addition to handling files, we will also see different file formats(.txt, .json, .xml, .csv, .tsv, .excel) in this section. First, let us get familiar with handling files with common file format(.txt).
File handling is an import part of programming which allows us to create, read, update and delete files. In Python to handle data we use open() built-in function.
open('filename', mode) # mode(r, a, w, x, t,b) could be to read, write, update
"r"
- Read - Default value. Opens a file for reading, it returns an error if the file does not exist"a"
- Append - Opens a file for appending, creates the file if it does not exist"w"
- Write - Opens a file for writing, creates the file if it does not exist"x"
- Create - Creates the specified file, returns an error if the file exists"t"
- Text - Default value. Text mode"b"
- Binary - Binary mode (e.g. images
Opening Files for Reading#
The default mode of open is reading, so we do not have to specify “r” or “rt”. I have created and saved a file named reading_file_example.txt in the files directory. Let us see how it is done:
f = open("reading_file_example.txt")
print(f)
<_io.TextIOWrapper name='reading_file_example.txt' mode='r' encoding='UTF-8'>
As you can see in the example above, we printed the opened file and it gave some information about it. Opened file has different reading methods: read()
, readline()
, and readlines()
. An opened file has to be closed with close()
method.
read()#
read()
reads the whole text as string. If we want to limit the number of characters we want to read, we can limit it by passing int value to the read(number) method.
f = open("reading_file_example.txt")
text = f.read()
print(text)
f.close()
Halo, ini adalah contoh membaca file dengan Python.
Sebagai bagian dari training Python oleh Lab Fasilkom Unsri.
I <3 Python.
Instead of printing all the text, let us print the first 10 characters of the text file.
f = open("reading_file_example.txt")
text = f.read(10)
print(text)
f.close()
Halo, ini
readline()#
readline()
reads only the first line.
f = open("reading_file_example.txt")
text = f.readline()
print(text)
f.close()
Halo, ini adalah contoh membaca file dengan Python.
readlines()#
readlines()
reads all the text line by line and returns a list of lines
f = open("reading_file_example.txt")
lines = f.readlines()
print(lines)
f.close()
['Halo, ini adalah contoh membaca file dengan Python.\n', 'Sebagai bagian dari training Python oleh Lab Fasilkom Unsri.\n', 'I <3 Python.']
Another way to get all the lines as a list is using read().splitlines()
:
f = open("reading_file_example.txt")
lines = f.read().splitlines()
print(lines)
f.close()
['Halo, ini adalah contoh membaca file dengan Python.', 'Sebagai bagian dari training Python oleh Lab Fasilkom Unsri.', 'I <3 Python.']
After we open a file, we should close it. There is a high tendency of forgetting to close them. There is a new way of opening files using with - closes the files by itself. Let us rewrite the the previous example with the with method:
with open("reading_file_example.txt") as f:
lines = f.read().splitlines()
print(lines)
['Halo, ini adalah contoh membaca file dengan Python.', 'Sebagai bagian dari training Python oleh Lab Fasilkom Unsri.', 'I <3 Python.']
Opening Files for Writing and Updating#
To write to an existing file, we must add a mode as parameter to the open() function:
"a"
- append - will append to the end of the file, if the file does not it creates a new file."w"
- write - will overwrite any existing content, if the file does not exist it creates.
Let us append some text to the file:
with open("appending_file_example.txt", "a") as f:
f.write("This text has to be appended at the end.\n")
Verify if the text appended to the file:
with open("../../../data/writing_file_example.txt") as f:
text = f.read()
print(text)
This text has to be appended at the end.
The method below creates a new file, if the file does not exist:
with open("creating_file_example.txt", "w") as f:
f.write("This text will be written in a newly created file.")
Deleting Files#
We have seen in previous section, how to make and remove a directory using os module. Again now, if we want to remove a file we use os module.
import os
os.remove('./files/example.txt')
If the file does not exist, the remove method will raise an error, so it is good to use a condition like this:
import os
if os.path.exists('./files/example.txt'):
os.remove('./files/example.txt')
else:
print('The file does not exist')
File Types#
File with .csv
Extension#
CSV stands for comma separated values. CSV is a simple file format used to store tabular data, such as a spreadsheet or database. CSV is a very common data format in data science.
"name","country","city","skills"
"Aurora","Indonesia","Palembang","Python"
import csv
with open("../../../data/csv_example.csv") as f:
csv_reader = csv.reader(f, delimiter=",") # w use, reader method to read csv
line_count = 0
for row in csv_reader:
if line_count == 0:
print(f'Column names are: {", ".join(row)}')
line_count += 1
else:
print(f"\t{row[0]} is a student. He lives in {row[2]}, {row[1]}.")
line_count += 1
print(f"Number of lines: {line_count}")
Column names are: name, country, city, skills
Aurora is a student. He lives in Palembang, Indonesia.
Jokowi is a student. He lives in Solo, Indonesia.
Number of lines: 3
with open("../../../data/csv_example.csv") as f:
lines = f.readlines()
line_count = 0
for line in lines:
if line_count == 0:
print(f"Column names are: {line}")
else:
row = line.split(",")
print(f"\t{row[0]} is a student. He lives in {row[2]}, {row[1]}.")
line_count += 1
Column names are: "name","country","city","skills"
"Aurora" is a student. He lives in "Palembang", "Indonesia".
"Jokowi" is a student. He lives in "Solo", "Indonesia".