What Is File:: Chapter 4: Data File Handing (Part 1)
What Is File:: Chapter 4: Data File Handing (Part 1)
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.
Advantanges of file:
Data stored permanently.
Stored data can be shared.
We can modify data later on.
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 function 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. This method takes string type data.
Syntax: handle.write(string)
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.
Seek () can be done in two ways:
For text file: f.seek(location)
Here, seek () always starts from beginning.
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, 1: Means current position, 2: Means end position.
Tell(): This function is used to return current position of file pointer, Example: p.tell().
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()
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')
Chapter 4: Data file handing (Part 3)
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)
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(dataojbject, 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()
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')
Chapter 4: Data file handing (Part 5)
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.
SN, Name, City
1, Michael, New Jersey
2, Jack, California
To represent a CSV file, it must be saved with the .csv file extension.