Friday, July 3, 2020

file handling programs in python ,for class 12 cbse


filepractice
In [ ]:
def filecopy(f1,f2):
    fp=open(f1,'r')
    fp1=open(f2,'w')
    l=fp.readline()
    while l!='':
        fp1.write(l)
        l=fp.readline()
    fp.close()
    fp1.close()
In [2]:
import os
print(os.getcwd())
C:\Users\Acer
In [7]:
filecopy('corona.txt.txt','covid.txt')
In [8]:
def filecpy(f1,f2):
    fp=open(f1,'r')
    fp1=open(f2,'w')
    l=fp.read()
    fp1.write(l)
    fp.close()
    fp1.close()
In [9]:
filecpy('covid.txt','covid19.txt')
In [10]:
def fileread(f):
        fl=open(f)
        print(fl.read())
        
In [11]:
fileread('covid19.txt')
corona virus ,keep safe yourself,protect yourself keep yourself quarantine
In [13]:
fileread('corona.txt.txt')
corona virus ,keep safe yourself,protect yourself keep yourself quarantine
In [14]:
fileread('covid19.txt')
corona virus ,keep safe yourself,protect yourself keep yourself quarantine
In [15]:
def filereadlines(f):
        fl=open(f)
        print(fl.readlines())  
In [16]:
filereadlines('covid19.txt')
['corona virus ,keep safe yourself,protect yourself keep yourself quarantine']
In [18]:
def filereadline(f):
        fl=open(f)
        l=fl.readline()
        while l!='':
            print(l)
            l=fl.readline()
        
        fl.close()    
In [3]:
def countline(f):
        py=0
        fl=open(f)
        l=fl.readline()
        while l!='':
            print(l)
            py+=1
            l=fl.readline()
        print('number of lines in file is ',py)
        fl.close()    
In [4]:
countline('covid.txt')
corona virus 

keep safe yourself

protect yourself 

keep yourself quarantine
number of lines in file is  4
In [ ]:
def exam():

No comments:

Post a Comment