0% found this document useful (0 votes)
16 views10 pages

File Handing For Students

Chapter 4 discusses file handling in Python, explaining the definition, uses, and advantages of files, as well as the various types of files including text, binary, and CSV. It outlines the steps for file handling, including opening, performing operations, and closing files, along with the modes for opening files and methods for reading and writing data. Additionally, it covers the use of the pickle module for binary file operations and the CSV module for handling CSV files.

Uploaded by

chamritrnd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views10 pages

File Handing For Students

Chapter 4 discusses file handling in Python, explaining the definition, uses, and advantages of files, as well as the various types of files including text, binary, and CSV. It outlines the steps for file handling, including opening, performing operations, and closing files, along with the modes for opening files and methods for reading and writing data. Additionally, it covers the use of the pickle module for binary file operations and the CSV module for handling CSV files.

Uploaded by

chamritrnd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

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 4) There is no delimiter for a line.
(EOL) also called (‘\n’).
4) Text files are having extension like: 4) Binary file are having extension like:
.txt, .csv, .rtf . .exe 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(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.
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()

Methods of OS module:
1. The rename() method used to rename the file.
Syntax os.rename(current_file_name, new_file_name)
2. The remove() method to delete file.
Syntax os.remove(file_name)
3.The mkdir() method of the os module to create directories in the current directory.
Syntax os.mkdir("newdir")
4.The chdir() method to change the current directory.
Syntax os.chdir("newdir")
5.The getcwd() method displays the current directory.
Syntax os.getcwd()
6. The rmdir() method deletes the directory.
Syntax os.rmdir('dirname')

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
import os
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

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


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


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

Delete record in a binary file


def delete():

file=open('demo.txt','rb')
file2=open('demo2.txt','wb')
roll=int(input('Enter roll no.:'))
try:
while True:

i=pickle.load(file)
if i[0]!=roll:
pickle.dump(i,file2)

except EOFError:
print("")
file.close()
file2.close()
os.remove('demo.txt')
os.rename('demo2.txt','demo.txt')

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