Sunday, July 5, 2020

Python Text File Program and Practice 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 [5]:
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 [1]:
def filereadline(f):
        fl=open(f)
        l=fl.readline()
        while l!='':
            print(l)
            l=fl.readline()
        
        fl.close()    
In [2]:
filereadline('covid19.txt')
corona virus ,keep safe yourself,protect yourself keep yourself quarantine
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 [2]:
s = 'testing 1 2 testing'

d = {}
for word in s.split():
    word = int(word) if word.isdigit() else word
    if word in d:
        d[word] += 1
    else:
        d[word] = 1
print(d)
{'testing': 2, 1: 1, 2: 1}
In [3]:
print(s.split())
['testing', '1', '2', 'testing']
In [6]:
"sd sd fdgf dfgdfg".split()
Out[6]:
['sd', 'sd', 'fdgf', 'dfgdfg']
In [15]:
def readtextbin(fil):
    f=open(fil,'rb')
    import pickle
    data=pickle.load(f)
    print(data)
    f.close()
In [16]:
readtextbin('covid.txt')
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-16-c26caf284c84> in <module>
----> 1 readtextbin('covid.txt')

<ipython-input-15-2b97f899b242> in readtextbin(fil)
      2     f=open(fil,'rb')
      3     import pickle
----> 4     data=pickle.load(f)
      5     print(data)
      6     f.close()

ModuleNotFoundError: No module named 'orona virus '
In [8]:
fobj_in = open("covid19.txt")
fobj_out = open("covid2019.txt","w")
i = 1
for line in fobj_in:
    print(line.rstrip())
    fobj_out.write(str(i) + ": " + line)
    i = i + 1
fobj_in.close()
fobj_out.close()
corona virus ,keep safe yourself,protect yourself keep yourself quarantine
help to save india from corona
protect yourself protect india
In [6]:
fileread('covid2019.txt')
1: corona virus ,keep safe yourself,protect yourself keep yourself quarantine
2: help to save india from corona
3: protect yourself protect india

In [15]:
def filecountword(fl):
    fc=open(fl)
    count=0
    for line in fc:
        print(line.split())
        print(len(line.split()))
        count+=len(line.split())
    print('number of word in file is',count)
    fc.close()
In [16]:
filecountword('covid2019.txt')
['1:', 'corona', 'virus', ',keep', 'safe', 'yourself,protect', 'yourself', 'keep', 'yourself', 'quarantine']
10
['2:', 'help', 'to', 'save', 'india', 'from', 'corona']
7
['3:', 'protect', 'yourself', 'protect', 'india']
5
number of word in file is 22
In [ ]:
def filecountword(fl):
    fc=open(fl)
    count=0
    fc.filecount
    print('number of word in file is',count)
    fc.close()