0% found this document useful (0 votes)
8 views

File Handing Notes for Students

Uploaded by

thebramhansh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

File Handing Notes for Students

Uploaded by

thebramhansh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Chapter 4: Data file handing

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.

What is file handling:


File handling in Python enables us to create, update, read, and delete the files stored on the file
system through our python program. The following operations can be performed on a file:
In Python, File Handling consists of following three steps:
 Open the file.
 Performing operation (Read, Write, Append, Insert, Delete, etc.) or Processing data.
 Close the file.

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 (;).

Text file Vs Binary file:


Text File Binary file
1) Data is stored in ASCII or Unicode 1) Data is stored as it is in memory location
2) Data is in human readable form. 2) Data is in not human readable form.
3) Execution is slow. 3) Execution is fast.
4) Each line is terminated End of Line (EOL) 4) There is no delimiter for a line.
also called (‘\n’).
4) Text files are having extension like: .txt, .csv, 4) Binary file are having extension like: .exe
.rtf . files,mp3 file, image files, word documents.

Opening text file:


Open(): This function used to open any file. It takes file name as the first argument and mode of
accessing the file as the second argument.
Syntax: File_variable/ file_object / handle= open (file_name , access_mode)
Example: f = open("demo5.txt")
Absolute Path Relative Path
It is a full path of the file from the root directory. It is the path of the file from the
current working directory.
f=open(r"C:\Users\quick\Desktop\demo5.txt") open("demo5.txt")
f=open("C:\\Users\\quick\\Desktop\\demo5.txt")
Properties of File Object: Name, Mode, Closed and Encoding.

Closing text file:


Close():
 A close() function breaks the link of file object and the file on the disk. Close() is also used to
flush data from buffer to file.
 After using this no tasks can be performed on that file through the file-object.
 Example: f = close ( )
Buffer area: Buffer is temporary memory where data stores before being written to the file.
Flush() : Flush () is used to force transfer of data from buffer to file before using close().

Opening text file using ‘with’ statement:


Syntax: with open(file_name,acces_mode) as file_object:
file_object.read()
(Need not to use close() to close file)
Example:
with open(‘demo5.txt’,r) as f:
f.read()

Modes to open files:


r Opens a file for reading only. The file pointer is placed at the beginning of the file. This is
default mode. If the file specified file does not exist, it will generate FileNotFoundError.
w Opens file for writing only. Overwrites the file if the file exists. If the file does not exist, it
creates a new file for writing.
a Opens file for appending. The file pointer is at the end of the file if the file exists.
If the file does not exist, it creates a new file for writing.
r+ Open file for both reading and writing. The file pointer will be at the beginning of the file.
w+ Open file for both writing and reading. Overwrites the file if the file exists. If the file does
not exist, it creates a new file.
a+ Open file for both appending and reading. The file pointer is at the end of the file if the
file exists. If the file does not exist, it creates a new file.
rb Opens a file for reading only in binary format. The file pointer is placed at the beginning of
the file.
wb Opens file for writing only in binary file. Overwrites the file if the file exists. If the file does
not exist, it creates a new file for writing.
ab Opens file for appending in binary file. The file pointer is at the end of the file if the file
exists. If the file does not exist, it creates a new file for writing.
rb+ Opens a file for reading and writing only in binary format. The file pointer is placed at the
beginning of the file.
wb+ Opens file for writing and reading in binary file. Overwrites the file if the file exists. If the
file does not exist, it creates a new file for writing.
ab+ Opens file for appending and reading in binary file. The file pointer is at the end of the file
if the file exists. If the file does not exist, it creates a new file for writing.

Modes to open binary files:


r, w , a, r+, w+, a+

How to Read Data from Text file:


 read()
 readline()
 readlines()
Read(): This function is used to read all data of the file. And send file pointer to end. Read data as
string.
Read(n): Used to read ‘n’ numbers of characters from the file. And send file pointer to ‘n’ position.
Syntax: handle.read() or handle.read(n)
Readline(): This function is used to read only one line from the file. Read data as string.
Syntax: handle.readline()
Readlines(): This function is used to read all lines from the file into list.
Syntax: handle.readline()

How to Write/ Appending data in a text file:


 Write()
 Writelines()

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()

Random access in files using Seek() and Tell():


Seek(): This function is used to change the position of the file handle (file pointer) to a given
specific position.
File Pointer: File pointer is like a cursor, which defines from where the data has to be read or
written in the file.
Seek () can be done in two ways:
 Absolute Positioning (for text file): f.seek(location)
Here, seek () always starts from beginning.
Ex: f.seek(5)
 Relative Positioning (for binary file): f.seek(offset, from_what)
Offset: This argument is used to move file pointer.(-10 for backward and 10 for forward)
From_what: This argument is used to define reference point. Like,
0: Means absolute file positioning(from starting),
1: Means current position,
2: Means end position.
Ex: f.seek(-6,2)

Tell(): This function is used to return current position of file pointer, Example: p.tell().
*Use of r+, w+, a+

Manipulation of data in a text file:


This concept is fully based on overwriting of data.
Example:
p=open('demo2.txt','r')
k=p.read()
data=k.swapcase()
p.close()

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()

Open and Close binary files:


We can open and close binary file same as text file using open() and close().
Syntax: File_variable/ file_object / handle= open(file_name,access_mode)

Modes to open binary files:


rb, wb , ab, rb+, wb+, ab+

Reading and writing data in Binary files:


Pickle Module: This module provides us the ability to serialize and de-serialize objects .
Pickling: Pickling is the process of converting the structure to a byte stream (machine readable)
Before writing to a binary file. This is also called data serialization.
dump() method is used for pickling. This is used for writing in binary file.

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()

Code to read from binary file using load():


p=open('demo.dat','rb')
print(pickle.load(p))
print(pickle.load(p))
p.close()

Writing multiple record in a binary file


import pickle
def write():
file=open('demo.txt','ab')
y='y'
while y=='y':
roll=int(input('Enter roll no.:'))
name=input('Enter name:')
marks=int(input('Enter Marks:'))
data=[roll,name,marks]
pickle.dump(data,file)
y=input('Do you want to enter data again:')
file.close()

Read multiple record in a binary file


import pickle
def read():
file=open('demo.txt','rb')
try:
while True:
i=pickle.load(file)
print(i)
except EOFError:
print()
file.close()

Search record in a binary file


import pickle
def search():
file=open('demo.txt','rb')
roll=int(input('Enter roll no.:'))
try:
while True:
i=pickle.load(file)
if i[0]==roll:
print(i)
break
except EOFError:
print()
file.close()

Update record in a binary file


import pickle
def update():
file=open('demo.txt','rb+')
roll=int(input('Enter roll no.:'))
try:
while True:
cpos=file.tell()
i=pickle.load(file)
if i[0]==roll:
i[2]=int(input('Enter New marks:'))
file.seek(cpos)
pickle.dump(i,file)
break
except EOFError:
print()
file.close()

What is CSV file:


A CSV (Comma Separated Values) format is one of the most simple and common ways to store
tabular data. The information is organized with one record on each line and each field is separated
by comma. To represent a CSV file, it must be saved with the .csv file extension.

Features if CSV file:


 A CSV stands for Comma Separated Values.
 Each line of file is called record.
 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 (;).

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()

You might also like