File Handling
#to use read function first you have to create a text file ,write some lines in notepad and save it
#If text file is saved in python main module(installation) path
'''f=open("[Link]")
print([Link]())
print("reading lines")'''
#OR use variable a for read fuction
'''f=open("[Link]", "r")
a=[Link]()
print(a)
print("reading lines")'''
#If text file is saved in any other location like desktop(copy and paste path between quotes)
# use raw(r)mode in starting of path before quotes or \\ to read the file
f=open(r"C:\Users\BABA PC\Desktop\[Link]", "r")
print([Link]())
print("Reading lines")
# ORuse this \\
'''f=open("C:\\Users\\BABA PC\\Desktop\\[Link]", "r")
print([Link]())
print("Reading lines")'''
#4. type no. of characters which you want to read in read() function
# type function is used only if you are using any variable to print the file handle
'''f=open("[Link]", "r")
a=[Link](50)
print(a)
print("Reading characters")
print("type=", type(a))'''
# readline() function read only one line of any paragraph
'''f=open("[Link]", "r")
a=[Link]()
print(a)
print("Reading only one line")
print("type=", type(a))'''
# if you want to read one line after oneline use readline() function with print function
'''f=open("[Link]", "r")
a=[Link]()
print(a)
a=[Link]()
print(a)
a=[Link]()
print(a)
print("Reading only one line after one line")
print("type=", type(a))'''
# if you want to read one line after oneline and each line has some specific
# characters use readline(no. of characters) function with print function
'''f=open("[Link]", "r")
a=[Link](6)
print(a)
a=[Link](8)
print(a)
a=[Link](10)
print(a)
print("Reading only one line after one line with define no. of characters in function")
print("type=", type(a))'''
# readlines() function return the whole paragraph as a list with each line ends with \n
'''f=open("[Link]", "r")
a=[Link]()
print(a)
print("Reading lines")
print("type=", type(a))'''
# if we want to print all the lines without /n in each line as list use for loop
'''f=open("[Link]", "r")
a=[Link]()
for i in a:
print(i)
print("Reading lines")
print("type=", type(a))'''
# Use of write() function, save your python file in computer not pendrive
# If your text file is not present it will create text file
#Text file will be created in that place where this file saved
# This code will not write any text in the file because close() function is not used
'''f=open("[Link]", "w")
[Link]("computer science")'''
# This code will write data in the file by close() function
'''f=open("[Link]", "w")
[Link]("Computer Science with Python")
[Link]()
print("Writing Lines in text file")
print("type=", type(f))'''
# if you dont want to write close() function use with() function
'''with open("[Link]", "w") as f:
[Link]("welcome to the python programing")
print("writing line")'''
# If you want to type another text in the place of previous text use this code
#But you previous text will be removed and the new text will show on that place
'''f=open("[Link]", "w")
[Link]("Welcome to Python")
[Link]()
print("Writing Lines in text file")
print("type=", type(f))'''
# If you want to write another line use this code with \n in the starting of text
# If you will not use \n before text then another line text will show in the same 1st line
'''f=open("[Link]", "w")
[Link]("\nComputer Science with Python")
[Link]("\nHello welcome to the python")
[Link]("\nCoding in Python")
[Link]("\npython is programing language")
[Link]()
print("Writing Lines in text file")
print("type=", type(f))'''
# if you want to write more then on line in one function use writelines()
# this function will work in string, list and tuple
'''f=open("[Link]", "w")
list=['computer science\n', 'maths\n', 'english\n','physics']
[Link](list)
[Link]()
print("Writing Lines in text file")
print("type=", type(f))'''
# OR use tuple
'''f=open("[Link]", "w")
t=('computer science\n', 'sst\n', 'english\n','python')
[Link](t)
[Link]()
print("Writing Lines in text file")
print("type=", type(f))'''
# sum programe in file
'''f=open("[Link]", "a")
a=int(input("enter 1st number"))
b=int(input("enter 2nd number"))
c=a+b
[Link]('A= '+str(a))
[Link]('\tB= '+str(b))
[Link]('\tAddition= '+str(c)+ '\n\n')
[Link]()'''
# use of r+ mode, in this mode file pointer is at the begining of the file
'''f=open("[Link]", "r+")
print([Link]())
[Link]("ABC")
[Link]()'''
# OR r+ but this last read () will not work because file pointer is in the last of line
# so pointer can not read backword line
'''f=open("[Link]", "r+")
print([Link]())
[Link]("ABC")
print([Link]())
[Link]()'''
# use seek() function after write () to move the pointer at begining of line
# Now it can easily read() the data then write() the data then again read the previous line
'''f=open("[Link]", "r+")
print([Link]())
[Link]("ABC")
[Link](0)
print([Link]())
[Link]()'''
# After read() use seek(5) function to move the pointer at 5th position of line
# Now write some data then again use seek(0) and the read() whole data
'''f=open("[Link]", "r+")
print([Link]())
[Link](5)
[Link]("BABA INTERNATIONAL SCHOOL")
[Link](0)
print([Link]())
[Link]()'''
# Use tell() function to know the pointer current poition after reading data
'''f=open("[Link]", "r")
print("Initially file pointer position is at:",[Link]())
print("It will read 5 bytes:", [Link]())
print("After reading file pointer position is at:",[Link]())
print("reading data")'''
# Use tell() function to know the pointer current poition after writing data
'''f=open("[Link]", "w")
[Link]("Python Programing")
print("After writing file pointer position is at:",[Link]())
print("writing data")
[Link]()'''
# use of W+ mode
'''f=open("[Link]", "w+")
[Link]("Welcome to Python")
[Link](0)
print([Link]())
[Link]()
print("Writing Lines in text file")
print("reading data")
print("type=", type(f))'''
# use of a+ mode
'''f=open("[Link]", "a+")
[Link]("\nWelcome to Python")
[Link](0)
print([Link]())
[Link]()
print("Writing Lines in text file")
print("reading data")
print("type=", type(f))'''
# Standard Input/Output
# [Link]()
#[Link]()
#[Link]()
'''import sys
[Link]("Enter any value")
a=int([Link]())
[Link]("Enter any value")
b=int([Link]())
if a==0:
[Link]("Can't devide the numbers")
elif b==0:
[Link]("Can't devide the numbers")
else:
[Link]("Division")'''
# Read the text file by sys module (standard I/O)
'''import sys
[Link]("Enter the file name=")
file=[Link]()
f=open([Link](),"r")
while True:
ch=[Link](1)
if ch==' ':
[Link]("now pointer reached at the end of file....")
break
else:
print(ch, end=' ')
[Link]()'''
# use of strip () function and \n in the error
'''import sys
[Link]("Enter the file name=")
file=[Link]()
f=open(file,"r")
while True:
ch=[Link](1)
if ch==' ':
[Link]("now pointer reached at the end of file....")
break
else:
print(ch, end=' ')
[Link]()'''
# Absolule Path C:\Users\BABA PC\Desktop\Folder1\Folder 2\[Link]
# Now we create folder 1 then create folder 2 inside folder 1 on desktop
# Now create a text file with some data in the last(folder 2)
# save the python file in folder 1
f=open(r'C:\Users\BABA PC\Desktop\Folder1\Folder 2\[Link]', 'r')
print([Link]())
print("reading lines")
# Relative Path
# Now we create folder 1 then create folder 2 inside folder 1 on desktop
# Now create a text file with some data in the last(folder 2)
# save the python file in folder 1
f=open(r'.\Folder 2\[Link]', 'r')
print([Link]())
print("reading lines")
# Relative Path with two dots..
# create a folder on desktop and save the text file in this
f=open(r'..\hello\[Link]', 'r')
print([Link]())
print("reading lines")
# Binary files using pickling and unpickling
#Pickling refers to the process of converting the structure of
#(List, Dictionary) to a byte stream before writing in file
# Unpickling is process of converting byte stream back to original structure
# we can write the data in binary files by dump() function
#[Link](structure,file object))
#structure=[Link](file object)
# we can read the data from binary file by load() function
'''import pickle
def abc():
f=open("Binary [Link]", 'wb')
list=['Comp. science', 'Maths', 'English', 'Physics', 'Chemistry']
[Link](list,f)
[Link]()
abc()
print("Data written in file succesfully...............")'''
# Write and read data from Binary files.
'''import pickle
def write():
f=open("[Link]", "wb")
list=['Comp. science', 'Maths', 'English', 'Physics', 'Chemistry','sst']
[Link](list,f)
[Link]()
def read():
f=open("[Link]", "rb")
list=[Link](f)
print(list)
[Link]()
print("Data written in file succesfully...............")
write()
print("Reading data....................")
read()'''
# Read and write data(List and Dictionary) in Binary file
'''import pickle
def write():
f=open("Binary [Link]", "wb")
list=['Comp. science', 'Maths', 'English', 'Phy.', 'Chem.', 'Bio.', 'Eco.']
dict={'Comp' :100, 'Math' : 98, 'Eng' : 95, 'Phy' : 79, 'chem' : 80, 'Bio': 75, 'Eco': 89}
[Link](list,f)
[Link](dict,f)
[Link]()
def read():
f=open("Binary [Link]", "rb")
list=[Link](f)
dict=[Link](f)
print(list)
print(dict)
[Link]()
print("Data written in file succesfully...............")
print("Reading data....................")
write()
read()'''
# Read and write data without using def(write): and def(read): function
'''import pickle
f=open("[Link]", 'wb')
list=['Comp. science', 'Maths', 'English', 'Phy.', 'Chem.', 'Bio.', 'Eco.']
dict={'Comp' :100, 'Math' : 95, 'Eng' : 98, 'Phy' : 82, 'chem' : 84, 'Bio': 80, 'Eco': 95}
[Link](list,f)
[Link](dict,f)
[Link]()
print("Data written in file succesfully...............")
f=open("[Link]", "rb")
list=[Link](f)
dict=[Link](f)
print(list)
print(dict)
[Link]()
print("Reading data....................")'''
#using append mode
'''import pickle
f=open("[Link]", 'ab')
list=['Comp. science', 'Maths', 'English', 'Phy.', 'Chem.', 'Bio.', 'Eco.']
dict={'Comp' :100, 'Math' : 95, 'Eng' : 98, 'Phy' : 82, 'chem' : 84, 'Bio': 80, 'Eco': 95}
[Link](list,f)
[Link](dict,f)
[Link]()
print("Data written in file succesfully...............")
f=open("[Link]", "rb")
list=[Link](f)
dict=[Link](f)
print(list)
print(dict)
[Link]()
print("Reading data....................")'''
#nested list in binary file
'''import pickle
def write():
f=open("[Link]", "wb")
a=[ ]
while True:
rno=int(input("enter the roll no : "))
name=input("enter the name : ")
marks=int(input("enter the marks : "))
b=[ rno,name,marks]
[Link](b)
ch=input("Do you want to enter more records .. Y/N ")
if ch=='n' :
break
[Link](a,f)
[Link]()
def read():
f=open("[Link]", "rb")
a=[Link](f)
print(a)
[Link]()
write()
print("records writing successfully.")
read()
print("Reading data....................")'''
#nested list with for loop displayed as sequence with bracket
'''import pickle
def write():
f=open("[Link]", "wb")
a=[ ]
while True:
rno=int(input("enter the roll no : "))
name=input("enter the name : ")
marks=int(input("enter the marks : "))
b=[ rno,name,marks]
[Link](b)
ch=input("Do you want to enter more records .. Y/N ")
if ch=='n' :
break
[Link](a,f)
[Link]()
def read():
f=open("[Link]", "rb")
c=[Link](f)
for i in c :
print(i)
[Link]()
write()
print("records writing successfully.")
read()
print("Reading data....................")'''
#nested list with for loop displayed as sequence without bracket
import pickle
def write():
f=open("[Link]", "wb")
a=[ ]
while True:
rno=int(input("enter the roll no : "))
name=input("enter the name : ")
marks=int(input("enter the marks : "))
b=[ rno,name,marks]
[Link](b)
ch=input("Do you want to enter more records .. Y/N ")
if ch=='n' :
break
[Link](a,f)
[Link]()
def read():
f=open("[Link]", "rb")
c=[Link](f)
for i in c :
rno=i[0]
name=i[1]
marks=i[2]
print(rno,name,marks)
[Link]()
write()
print("records writing successfully.")
read()
print("Reading data....................")
#using append function
'''import pickle
def write():
f=open("[Link]", "wb")
a=[ ]
while True:
rno=int(input("enter the roll no : "))
name=input("enter the name : ")
marks=int(input("enter the marks : "))
b=[ rno,name,marks]
[Link](b)
ch=input("Do you want to enter more records .. Y/N ")
if ch=='n' :
break
[Link](a,f)
[Link]()
def append():
f=open("[Link]", "wb")
a=[ ]
while True:
rno=int(input("enter the roll no : "))
name=input("enter the name : ")
marks=int(input("enter the marks : "))
b=[ rno,name,marks]
[Link](b)
ch=input("Do you want to enter more records .. Y/N ")
if ch=='n' :
break
[Link](a,f)
[Link]()
def read():
f=open("[Link]", "rb")
c=[Link](f)
for i in c :
rno=i[0]
name=i[1]
marks=i[2]
print(rno,name,marks)
[Link]()
write()
print("records writing successfully.")
read()
print("Reading data....................")
append()
print("enter more data..")
read()
print("reading data agian..")'''
#Use of nested dictionary in binary file
'''import pickle
def write():
f=open("[Link]", "wb")
M={ }
while True:
r,m=eval(input("enter the roll no :, marks: "))
M[r]=m
ch=input("Do you want to enter more records .. Y/N ")
if ch=='n' :
break
[Link](M,f)
[Link]()
def append():
print(" do you want to enter more data..")
f=open("[Link]", "ab")
V={ }
while True:
r,m=eval(input("enter the roll no :, marks: "))
V[r]=m
ch=input("Do you want to enter more records .. Y/N ")
if ch=='n' :
break
[Link](V,f)
print(V)
[Link]()
def read():
f=open("[Link]", "rb")
b=[Link](f)
print(b)
[Link]()
write()
print("records writing successfully.")
read()
print("Reading data....................")
append()
read()
print("reading data agian..")'''
# CSV file operations:
# The seperator character of CSV files is called delimiter.
# Default delimiter is comma
# Other popular delimiters are (\t), (:), (|) and (;)