File Handing Notes for Students
File Handing Notes for Students
What is file:
A file is a sequence of character/bytes on the disk/permanent storage where a group of related
data is stored. File is created for permanent storage of data.
What is use of files:
File is created for permanent storage of data.
Advantanges of file:
Data stored permanently.
Stored data can be shared.
We can modify data later on.
Type of file:
1) Text File:
A file whose contents can be viewed using a text editor is called a text file.
A text file is simply a sequence of ASCII or Unicode characters.
Default character coding in python is ASCII, but using constant ‘u’ with string, it support
Unicode also.
In text file, each line is terminated by a special character known as End of Line (EOL) also
called (‘\n’). Example: .txt, .csv, .rtf .
2) Binary file:
A binary file stores the data in the same way as stored in the memory.
We can’t read a binary file using a text editor.
There is no delimiter for a line.
Example: .exe files,mp3 file, image files, word documents.
3) CSV file:
A CSV stands for Comma Separated Values.
CSV format is one of the most simple and common ways to store tabular data. To represent
a CSV file, it must be saved with the .csv file extension.
CSV is just like text file, contents can be viewed using a text editor is called a text file.
The default delimiter or separator is (,) comma. Other delimiters are tab(‘\t’),
Colon(:),pipe(|), and semicolon (;).
Write():
This method takes string type data. It is used to write single line in text file.
If file doesn’t exist, it will create the new file. And if file is exist it overwrite the data.
For storing data in new line we will have to add ‘\n’ (2 bytes) end of line character.
Syntax: handle.write(string)
For storing numeric data value in text file, conversion to string is required.
Writelines(): This function is used to write multiple data in form of sequence data type(string, list
and tuple).
Syntax: handle.readline()
Tell(): This function is used to return current position of file pointer, Example: p.tell().
*Use of r+, w+, a+
p=open('demo2.txt','w')
k=p.write(data)
p.close()
Second Way
p=open('demo5.txt','r+')
k=p.read()
data=k.swapcase()
p.seek(0)
k=p.write(data)
p.close()
Unpickling: Unpickling refers to a reverse process where the byte stream is converted to original
structure(Human readable). This is also called data de-serialization.
load() method is used for pickling. This is used for reading in binary file.
Syntax to import pickle module: import pickle
Syntax to used dump() method: pickle.dump(data/data object, fileobject / filehandle)
Syntax to used load() method: pickle.load(fileobject / filehandle)
*Note: We need to call load() each time dump() is called.
Code to Write in binary file using dump():
list1=[10,20,30,40,50]
list2={'One':10,'Two':20,'Three':30,'Four':40,'Five':50}
import pickle
p=open('demo.dat','wb')
pickle.dump(list1,p)
pickle.dump(list2,p)
p.close()
Why/Advantages of CSV:
1. When data has a strict tabular structure.
2. To import and export data for different types of databases.
3. CSV is faster to handle , smaller in size and easy to generate.
4. CSV is human readable and easy to edit manually.
5. CSV is processed by almost all existing applications.
Reading and Writing CSV file:
CSV module is used for reading and writing operations . It provide to method or function:
reader()- To read csv file.
writer() – To write csv file using writerow() and writerows().
(We can use delimiter=”,” parameter in writer() to change to delimiter.)
Open csv file for reading: f=open(“student.csv”,’r’)
(We can use newline=”\r\n” parameter to remove one line space after a row inserted.)
Open csv file for writing: f=open(“student.csv”,’w’)
(We can use newline=” ” parameter to remove one line space after a row inserted.)
Closing csv file for reading: f.close()