This Post is about using files with Python .
There are basically 3 operations we can perform :
Read
Write
Append
Reading Files
Reading from text file is also a 3 step process ie Open , Read and Close .
Simplest code to read a file is :
## open is passed with file_name and mode ('r' meaning read mode)
file = open("/Users/jatinmadaan/blog/file_io/sample.txt",'r')
## Looping through file object to print content
for line in file :
print (line)
file.close()
O/P :
Policy_id,Name,Age,Country,Plan_Type
111111,ABC1,20,India,Health1
111112,ABC2,30,India,Health2
111113,ABC3,20,India,Health1
111114,ABC4,30,India,Term1
Also we check and filter out data before printing as well :
## open is passed with file_name and mode ('r' meaning read mode)
file = open("/Users/jatinmadaan/blog/file_io/sample.txt",'r')
## Looping through file object to print content
for line in file :
## We can check for data here itself
if "111112" in line:
print(line)
file.close()
O/P :
111112,ABC2,30,India,Health2
Problem with using open () is that in case we forget to close the file then it leads to data corruption , also if in between there is any failure then file remains open (we can handle this with exceptional handling ).
To avoid above issue we can use with open function which will automatically take care of closing and avoids corruption of files .
eg :
with open("sample.txt",'r') as file :
for line in file :
if "111112" in line :
print (line)
O/P : 111112,ABC2,30,India,Health2
We can also use readline() to parse line by data
eg :
with open("sample.txt",'r') as file :
line = file.readline()
while line:
print(line)
line = file.readline()
O/P :
Policy_id,Name,Age,Country,Plan_Type 111111,ABC1,20,India,Health1 111112,ABC2,30,India,Health2 111113,ABC3,20,India,Health1 111114,ABC4,30,India,Term1
Here output contains extra spaces between lines , to remove those we can use end='' in print function .
eg :
with open(sample.txt",'r') as file
line = file.readline()
while line:
print(line,end='')
line = file.readline()
O/P :
Policy_id,Name,Age,Country,Plan_Type 111111,ABC1,20,India,Health1 111112,ABC2,30,India,Health2 111113,ABC3,20,India,Health1 111114,ABC4,30,India,Term1
There are also other functions eg readlines() and read() but they read entire file and put in memory which is very inefficient for large files.
2 . Writing to files
Mode used for writing is 'w' , in this mode file is overwritten if exists else a new file is created and data loaded into it.
eg : Let's create a list of fruits and store that data into a file
fruits=["Apple","Mango","Orange"]
with open("fruits_data.txt",'w') as fruits_data :
for line in fruits :
print(line,file=fruits_data)
O/P :
$ cat fruits_data.txt
Apple
Mango
Orange
3. Appending Data
Appending mode 'a' writes data at end of file , in case file doest not exists new file is created and data inserted.
eg :
new_policy="222222,DEF1,50,US,Term50"
with open("sample.txt",'a') as new_data :
print(new_policy,file=new_data)
Before running of code
$ tail -1 sample.txt
111121,ABC11,20,India,life3
After running of code
$ tail -1 sample.txt
222222,DEF1,50,US,Term50
Above are very basic and fundamental file operations to get starting with working on I/O in python using text files .
Comments