Chapter 6: File Handling - VinGok/Python-Basics GitHub Wiki
It is pretty common in any programming language to transact with data files . Python provides a rich set of features to interact (read and write) with files stored in computer memory.
.txt File handling functions
- open() is used to get access to a new or existing file
- read() is used to extract the contents of an existing file
- write() is used to write or append data into an existing file
- close() is used to clear file contents from cache
Writing to a .txt file
## writing into 'sample.txt' file
file = open('sample.txt','w')
text = "We are currently studying file handling.\nThis is an example to demonstrate file write."
file.write(text)
file.close()
If a file is present, write replaces the contents with new ones. Otherwise, a new file is created.
write() takes only string as an argument.
Reading from a .txt file
file = open('sample.txt', 'r')
text = file.read()
print(text)
file.close()
read() returns a string.
If a mode is not specified, the file is open in read mode by default.
Reading a file line-by-line
file = open('sample.txt')
while True:
text = file.readline()
if (text):
print("new line: {0}".format(text))
else:
break
readline() returns a string.
Reading a file into a list
file = open('sample.txt', 'w')
file.write("""This is a random statement
which is simply trying to fill multiple lines in a
file for demonstrating reading file contents""")
file.close()
file = open('sample.txt')
text = file.readlines()
print(text)
file.close()
readlines() returns a list.
Append an existing .txt file - pointer at the end of the file.
file_append = open('sample.txt','a')
text_append = "\nAppending a new line to the existing file."
file_append.write(text_append)
file_append.close()
file_read = open('sample.txt')
text_read = file_read.read()
print(text_read)
file_read.close()
If the file is present, new contents are appended to the old ones. Otherwise, a new file is created.
If a file is kept open even after it's usage, then it unnecessarily consumes space in RAM.
Mode | Description |
---|---|
r | Read mode; pointer at the start |
w | Write mode; pointer at the start |
a | Append mode; pointer at the end |
w+ | Write and read mode; pointer at the start |
a+ | Append and read mode; pointer at the end |
Write and read a file simultaneously.
file = open('sample.txt','w+')
text = "We are currently studying file handling.\tThis is an example to demonstrate file write."
file.write(text)
file.seek(0)
text = file.read()
print(text)
file.close()
Automatic file closing after reading/writing.
file = open('sample.txt')
text = file.read()
print(text)
print("File is closed: ", file.closed)
with open('sample.txt') as file:
text = file.read()
print(text)
print("File is closed: ", file.closed)
Programming Practice
1. The file movies.txt consists of movie names, year of release, and their genres. Write a Python program to separately count the number of movies
(i) with the genre comedy,
(ii) released from 1980 to 2005.
Handling CSV files
CSV - Comma Separated Values.
This format is typically used while downloading data from a database directly into a file. It uses commas as delimiters between fields.
Database
ID | Firstname | Surname | Title | Address | Country |
---|---|---|---|---|---|
1 | Polly | Parrot | Miss | 42 The Lane | CZ |
2 | Mabel | Canary | Mrs | 24 The Street | SK |
3 | Zöe | Zebra | Ms | 856 The Avenue | IN |
4 | José | Arara | Miss | Nenhuma Rua | OS |
Equivalent CSV format
ID,Firstname,Surname,Title,Address,Country
1,Polly,Parrot,Miss,42 The Lane,CZ
2,Mabel,Canary,Mrs,24 The Street,SK
3,Zöe,Zebra,Ms,856 The Avenue,IN
4,José,Arara,Miss,Nenhuma Rua,OS
Reading a CSV file.
import csv
with open ('tables.csv') as csvFile:
csvRead = csv.reader(csvFile, delimiter = ',')
for names in csvRead:
print(names)
Example: Find out the country code of Mrs. Canary.
import csv
with open ('tables.csv') as csvFile:
csvRead = csv.reader(csvFile, delimiter = ',')
for names in csvRead:
if (names[2].lower() == "canary"):
print("{0} is the country code of Mrs. {1}".format(names[-1], names[2]))
break
Reading with a different delimiter than comma.
id:firstnames:surname:title:address:Country
1:Polly:Parrot:Miss:42 The Lane:CZ
2:Mabel:Canary:Mrs:24 The Street:SK
3:Zöe:Zebra:Ms:856 The Avenue:IN
4:José:Arara:Miss:Nenhuma Rua:OS
import csv
with open ('tables.csv') as csvFile:
csvRead = csv.reader(csvFile, delimiter = ':')
for names in csvRead:
print(names)
Writing lists/tuples into a CSV file.
import csv
data = ["1","Polly","Parrot","Miss","42 The Lane","CZ"]
with open ('new_tables.csv', 'w') as csvFile:
csvWrite = csv.writer(csvFile, delimiter = ',')
csvWrite.writerow(data)
import csv
data = [["id","firstnames","surname","title","address","Country"],
["1","Polly","Parrot","Miss","42 The Lane","CZ"],
["2","Mabel","Canary","Mrs","24 The Street","SK"],
["3","Zöe","Zebra","Ms","856 The Avenue","IN"],
["4","José","Arara","Miss","Nenhuma Rua","OS"]]
with open ('new_tables.csv', 'w') as csvFile:
csvWrite = csv.writer(csvFile, delimiter = ',')
for names in data:
csvWrite.writerow(names)
Writing dictionary into a CSV file.
import csv
data = {"6":"Fred","Canary":"Mr","24 The Street":"AU"}
with open ('new_tables.csv', 'w') as csvFile:
csvWrite = csv.writer(csvFile, delimiter = ',')
for names in data:
csvWrite.writerow((names, data[names]))
Appending a CSV file.
import csv
data = [["6","Fred","Canary","Mr","24 The Street","AU"],
["7","Greg","Gerling","Mr","48","Street 2","IO"]]
with open ('new_tables.csv', 'a') as csvFile:
csvWrite = csv.writer(csvFile, delimiter = ',')
for names in data:
csvWrite.writerow(names)
import csv
data = ["6","Fred","Canary","Mr","24 The Street","AU"]
with open ('new_tables.csv', 'w') as csvFile:
csvWrite = csv.writer(csvFile, delimiter = ',')
for names in data:
csvWrite.writerow(names)
csvWrite.writerow("")
csvWrite.writerow("")
with open ('new_tables.csv', 'a') as csvFile:
csvWrite = csv.writer(csvFile, delimiter = ',')
csvWrite.writerow(data)