Python Code For Practicals.
fname = input('Enter file name=')
f1 = open(fname,'r')
s = f1.read()
print('The Content of file:')
print(s)
f1.close()
Output:
Enter file name=Text.txt
The Content of file:
Python is an interpreted,
high-level and
general-purpose
language.
Q2. write a program to accept the file name and count the number of lines in a file
fname = input("Enter the file name to count no of lines=")
f1 = open(fname,'r')
line = f1.readlines()
print(line)
print("no of lines=",len(line))
f1.close
Output:
Enter the file name to count no of lines=Text.txt
['Python is an interpreted, \n', 'high-level and \n', 'general-purpose \n', 'language.']
no of lines= 4
Q3. write a program to count no of character ,no of word and no of lines in a file. Accept filename from user.
fname = input("Enter the file name to count no of character, word and lines=")
f1 = open(fname, 'r')
s = f1.read()
print('Content of file:', s)
noc = len(s)
print('No of characters=', noc)
words = s.split()
print('Words in file', words)
now = len(words)
print('No of words=', now)
lines = s.split('\n')
print('Lines in file', lines)
nol = len(lines)
f1.close()
Output:
Enter the file name to count no of character, word and lines=Text.txt
Content of file: Python is an interpreted,
high-level and
general-purpose
language.
No of characters= 69
Words in file ['Python', 'is', 'an', 'interpreted,', 'high-level', 'and', 'general-purpose', 'language.']
No of words= 8
Lines in file ['Python is an interpreted, ', 'high-level and ', 'general-purpose ', 'language.']
Q4.Write a program to count no of vowels in a file.
fname = input("Enter the file name:")
f1 = open(fname,'r')
s = f1.read()
print(s)
vcnt = 0
vowels = ('a','e','i','o','u','A','E','I','O','U')
for ch in s:
if ch in vowels:
vcnt=vcnt+1
print('No of vowels=',vcnt)
Output:
Enter the file name:Text.txt
Python is an interpreted,
high-level and
general-purpose
language.
No of vowels= 21
Q5. write a program to count no of character, word in each line of file.
fname = input('Enter file name=')
f1 = open(fname,'r')
lines = f1.readlines()
i=1
for l in lines:
print('line no:',i,l)
i=i+1
print('No of characters=',len(l))
word=l.split()
print('No of words=',len(word))
f1.close()
Output:
line no: 1 Python is an interpreted,
No of characters= 27
No of words= 4
line no: 2 high-level and
No of characters= 16
No of words= 2
line no: 3 general-purpose
No of characters= 17
No of words= 1
line no: 4 language.
No of characters= 9
No of words= 1
Q6. write a program to count number of vowel in each line.
fname = input('Enter file name:')
f1 = open(fname,'r')
lines = f1.readlines()
print(lines)
i=1
vowel=('a','e','i','o','u','A','E','I','O','U')
for l in lines:
vcnt=0
for ch in l:
if ch in vowel:
vcnt=vcnt+1
print('In line no:',i,'No of Vowels=',vcnt)
i=i+1
Output:
['Python is an interpreted, \n', 'high-level and \n', 'general-purpose \n', 'language.']
In line no: 1 No of Vowels= 7
In line no: 2 No of Vowels= 4
In line no: 3 No of Vowels= 6
In line no: 4 No of Vowels= 4
Q7.Write a program to create a file of student data,store roll no and name of atleast 5 student.
f1 = open('Text.txt','w')
for i in range(5):
roll_no = input('Enter Roll no:')
name = input('Enter name:')
f1.write(roll_no+':'+name+'\n')
f1.close
Comments
Post a Comment