File Handling
File are of two types:
(i) Text file: are files which are ascii converted files.
Example: csv(comma separated values),tsv(tab separated values) and txt(text files)
(ii) Binary files: are file which raw digital data taken from input interface
without any ascii convertion.
Example: Any msoffice file(example .docx,.pptx,.xlsx etc) , bin(binary file),
dat(data file)
EOL : End of file. It is an character which marks the end of file or text document.
It generally noted as '\n\r' . \n means new line and \r means return(basically
enter key)
To open a file: we use open() function.
It takes three parameters :
(i) path: It takes two kinds of path.
(a) raw path: where you need to add r alphabet before the path name
enclosed within single quote or double quote
example:
f = open(r'C:/info/value.txt')
(b) absolute path: where you need to pass the path with escape sequences
\\.
f= open('C://info//value.txt')
(ii) mode: The default mode of open() function is read mode indicated by 'r'
The modes are of different types
(a) Text mode:
(i) r (read mode) : Here file is not created . Only read. If the
file is not found at the location. Then it gives FileNotFound Exception error.
Example:
f = open(r'C:/info/value.txt')
or
f= open('C://info//value.txt')
or
f= open('C://info//value.txt','r')
or
f = open(r'C:/info/value.txt','r')
(ii) w (write mode) : Here file if not present then it is created.
After creation the data is added to the file. If the data is already present. The
previous data is completely erased and new data is added to it.
Example:
f = open(r'C:/info/value.txt','w')
or
f= open('C://info//value.txt','w')
(iii) a (append mode): Here file if not present then if it is
created. After creation the data is added to the file. If the data is already present. Then where the EOL character is present from there it added new data. Thus
old information is not deleted.
Example:
f = open(r'C:/info/value.txt','a')
or
f= open('C://info//value.txt','a')
(iv)Extra modes are indicated by + symbol
(a) r+ (read and write)
(b) w+ (write and read)
(c) a+ (append and read)
(b) Binary mode :
Same as the text mode operation but 'b' is added to indicate
binary. example.
(i) f = open(r'C:/info/value.dat','ab') for append binary
(ii) f = open(r'C:/info/value.dat','rb') for read binary
(iii) f = open(r'C:/info/value.dat','wb') for write binary
(iv)Extra modes are indicated by + symbol
(a) rb+ (read and write)
(b) wb+ (write and read)
(c) ab+ (append and read)
(iii) buffering: The value of either 1 or 0. 1 indicates buffering is activated and
0 indicates buffering is deactivated.
Syntax:
f = open('path','mode','buffering')
Code:
f = open('C://info//value.txt','a',1) Here buffering is active.
Buffering is beneficial when a lot of data is getting stored in a file
continuously. So prevent lost
information. The consecutive informations are stored in high speed storage
space like RAM, in a ordered
manner. Once all the data is collected, chunks of data from RAM is passed
on to the file.
Text File are handled file(specifically file without .csv extension) are read using
(i) read(n): n indicates the number of character to read from the file.
(a) f = open('C://info//value.txt','r')
k = f.read()
This read() will read the entire content of the file into the
memory/RAM of the computer. So
if the data present in the file is huge , it may cause low
memory problems. Once the data is
read from the file, it is stored in the form of String.
(b) f = open('C://info//value.txt')
k = f.read(50)
When the number of characters specified into the read()
function, the read() function will only
read 50 characters from the file pointer position.
(ii) readline(): This function will read a single line ending with '\n'. Each time
is called.
So for instance. If the file contains suppose 10 lines. You have to call the
function 10 times
to display the entire text.
f = open('C://info//value.txt','r')
print(f.readline()) #displays the first line
print(f.readline()) #displays the second line
(iii) readlines(): This function wil read the entire text and store the text into a
list. Each line of the text is ended with '\n'. So suppose
value.txt file contains this:
Subhas Chandra Bose 23 January 1897 – 18 August 1945) was an Indian
nationalist whose
defiance of British authority in India made him a hero among many Indians.
The honorific
'Netaji' (Bengali: "Respected Leader") was first applied to Bose in Germany
in early
1942—by the Indian soldiers of the Indische Legion and by the German and
Indian officials
in the Special Bureau for India in Berlin. It is now used throughout India
So when the is opened.
f = open('C://info//value.txt','r')
k = f.readlines()
k will contain
k = ["Subhas Chandra Bose 23 January 1897 – 18 August 1945) was an Indian
nationalist whose\n","defiance of British authority in India made him a hero among
many Indians. The honorific\n","'Netaji' (Bengali: "Respected Leader") was first
applied to Bose in Germany in early","1942—by the Indian soldiers of the Indische
Legion and by the German and Indian officials\n","in the Special Bureau for India
in Berlin. It is now used throughout India\n']
Data is stored into the text file using write() and writelines() functions:
f = open('C://info//value.txt','w') or f = open('C://info//value.txt','a')
To use write functions the file mode must be either write(w) or append(a)
(a) write(): This function takes a string and stores into the file
k = "Learning Python"
f.write(k)
Store the data of variable k into the file value.txt
(b) writelines(): This functions takes a iterable(such as list,set or tuple) and
stores them into the text file.
k = ['Subhash Chandra Bose','Indian nationalist','Freedom Fighter'] #list
or
k = ('Subhash Chandra Bose','Indian nationalist','Freedom Fighter') #tuple
or
k = {'Subhash Chandra Bose','Indian nationalist','Freedom Fighter'} #set
f.writelines(k)
f.close()
#flush() in filehandling
Sometimes when data is being stored into the file. The data remains in a buffers
before being transfered to the file. The tranferring happens just before closing
the file. To immediately transfer from the buffer to the file, we need
to use the flush().
f = open('Zoom.txt','w')
f.write('Python programming was named after a film called Monty Python's
Flying Circus')
f.flush()
f.close()
Binary file handling:
To read and store data from and into the binary, we need to use the libary called
pickle. It is written as
import pickle
Why we need to use pickle module ?
We store different data types like list,tuple,string,set and dictionary, they have
different length. So it
difficult to store using normal text functions. Thus we need to serialize and
de-serialize the data types or objects.
Serialization means the process of convertion of data or object present in the
RAM/memory into binary data format also called binary streams, so that it can
stored into binary file or sent to network or database or disk.
De-serialization means the reverse process of serialization i.e. from the binary
streams back to the original object(example
list,tuple,dictionary,set,numbers,string etc).
To read data from a binary file we have two functions:
(a) load(): To open the binary and read the file. We can use any of the two
codeblock given below.
import pickle
f = open('info.dat','rb')
k = pickle.load(f)
print(k)
f.close()
or
import pickle
with open('info.dat','rb') as f:
k = pickle.load(f)
print(k)
load() reads the data present into file in the from of actual objects, i.e. if list
is present, list is read.
(b) loads(): reading serialised data from the binary file. It does not reads data
in the form of object rather it is read in the form of string.
*** loads() only works with serialized data. That is file stored with
dumps().
import pickle
f = open('info.dat','rb')
k = pickle.loads(f)
print(k)
f.close()
suppose info.dat contains this {'Ronit':1,'Debopriya':2,'Aawishko':3} and
k will store "{'Ronit':1,'Debopriya':2,'Aawishko':3}".
To bring back to the original dictionary
m = eval(k)
so m will contains {'Ronit':1,'Debopriya':2,'Aawishko':3}
or
import pickle
with open('info.dat','rb') as f:
k = pickle.loads(f)
print(k)
To store the data into binary file, we have two functions dump() and dumps()
(a)dump(): Store the object(tuple,list,dictionary,set,number,string) into the
binary file.
Syntax: pickle.dump(data to be stored,file pointer)
Example:
import pickle
k = {'Ronit':1,'Debopriya':2,'Aawishko':3}
f = open('students2023.dat','wb')
pickle.dump(k,f) #stores the object k into the binary file
f.close()
or
import pickle
with open('students2023.dat','wb') as f:
k = {'Ronit':1,'Debopriya':2,'Aawishko':3}
pickle.dumps(k,f)
(b)dumps(): It returs the object(tuple,list,dictionary,set,number,string) into the
serialised format i.e. string format binary format and store it.
import pickle
k = {'A':1,'B':2} #object
m = pickle.dumps(k) #convert the object to serialised data
f = open('info.dat','wb') #open the file for writing data
f.write(m) #write the serialised data into the file
f.close()
z = open('info.dat','rb') #open the file for reading
p = z.read() #read() reads the string present in the binary file
n = pickle.loads(p) #loads() converts the string back to the original
object
print(n)
z.close()
tell() and seek()
tell() --> return the index position(starting the 1) of file pointer.
f.tell() returns the cursor position
seek() --> changes the cursor position
f.seek(10) #cursor position has been changed to 10 character
CSV file (Comma Separated value)
To read and write CSV file we need
import csv #csv module
Example of a csv file content:
Name,Class,Roll
Ronit,12,1
Deepika,12,2
Debopriya,12,3
Aawishko,12,4
(i) To read the csv file we the following functions:
reader(): it is used to read csv files which opened as text files.
Syntax:
import csv
csv.reader(csv_file,delimiter)
delimeter is the separator each data. Example: , ; ' '(space) or
any other symbol
Example:
import csv
m = open('info.csv') #opens in read mode
k = csv.reader(m,delimiter=',') #here each data is separated by ,comma
for i in k:
print(i) #displays each row in a list format
Output:
['Name', 'Class', 'Roll']
['Ronit', '12', '1']
['Deepika', '12', '2']
['Debopriya', '12', '3']
['Aawishko', '12', '4']
(ii) To write the csv we need to use the following function:
writer()
Syntax:
import csv
with open('data.csv','w') as f:
data = csv.writer(f,delimeter=',')
k = int(input("How many rows to be stored: "))
for i in range(k):
name = input("Enter your name: ")
cls = int(input("Enter your class: "))
roll = int(input("Enter your roll number: "))
L=[name,cls,roll]
data.writerow(L)
Each list will be a row in the csv file