File Programs with Solutions on python.pptx

yogeshprasanna313 7 views 11 slides Jul 04, 2024
Slide 1
Slide 1 of 11
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11

About This Presentation

It's about python


Slide Content

LET’S TRY Write a Program to read the contents of mark.txt. Calculate the total marks and percentage by each student. Use readline ().split() . First line in the file contains the total number of students.

Solution fp1=open(" mark.txt "," r ") n= int (fp1.readline()) print("Total students:",n) for i in range(n): print("Student #",i+1,":",end="") allgrades =(fp1.readline().split()) print( allgrades ) sum=0 for j in range( len ( allgrades )): sum= sum+int ( allgrades [j]) per=float((sum/500)*100) print('Total=',sum,"\ nPercentage =",per) print("\n") OUTPUT: Total students: 5 Student # 1 :['60', '70', '80', '90', '100'] Total= 400 Percentage= 80.0 Student # 2 :['55', '65', '75', '85', '60'] Total= 340 Percentage= 68.0 Student # 3 :['70', '60', '80', '90', '67'] Total= 367 Percentage= 73.4 Student # 4 :['89', '76', '56', '43', '90'] Total= 354 Percentage= 70.8 Student # 5 :['67', '89', '76', '54', '90'] Total= 376 Percentage= 75.2

Exercise Write a Program to read the numbers from a file and count the number of even and odd numbers. Write a Program to read the contents from a file and count the number of occurrences of vowels in the content.

Write a Program to read the numbers from a file and count the number of even and odd numbers Program: fp =open (" numbers.txt","r ") lines= fp.readlines () evencount =0 oddcount =0 for i in lines: num = int ( i ) if num%2==0: evencount +=1 else: oddcount +=1 print( num ) print("No. of even numbers = ", evencount ) print("No. of odd numbers = ", oddcount ) fp.close () Output : Input: numbers.txt 11 12 13 14 15 No. of even numbers = 2 No. of odd numbers = 3

Write a Program to read the contents from a file and count the number of occurrences of vowels in the content. Program: fp =open(" exercise.txt","r ") lines= fp.read () print(lines) occur={'a':0,'e':0,'i':0,'o':0,'u':0} print("\ nCount of occurrences of vowels are: ") for char in lines: if char in ['a','e',' i ',' o','u ']: occur[char]=occur[char]+1 print(occur) fp.close () Output: Input: exercise.txt Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python has a design philosophy that emphasizes code readability, notably using significant whitespace. It provides constructs that enable clear programming on both small and large scales. Count of occurrences of vowels are: {'a': 26, 'e': 27, ' i ': 19, 'o': 15, 'u': 6}

Create a file numbers.txt with numbers stored in it. Write a program to read the file, check for odd and even numbers and write the odd and even numbers separately in two files namely odd.txt and even.txt. fp =open(" numbers.txt","r ") fp1=open(" odd.txt","w ") fp2=open(" even.txt","w ") lines= fp.readlines () for i in lines: num= int ( i ) if num%2==0: fp2.write("\n") fp2.write( str (num)) else: fp1.write("\n") fp1.write( str (num)) fp.close () fp1.close() fp2.close()

Write a Program to read the contents of “ mark.txt” . Calculate the total marks and percentage by each student. Use readline ().split() . First line in the file contains the total number of students. Required output to be written in a separate file called “ result.txt ” instead of printing in the screen. fp1=open(" mark.txt","r ") fp2=open("result.txt", "w") n= int (fp1.readline()) fp2.write("\ nTotal students:") fp2.write( str (n)) for i in range(n): allgrades =(fp1.readline().split()) fp2.write("\ nStudent #") fp2.write( str (i+1)) fp2.write( str ( allgrades )) sum=0 for j in range( len ( allgrades )): sum= sum+int ( allgrades [j]) per=float((sum/500)*100) fp2.write('\ nTotal =') fp2.write( str (sum)) fp2.write("\ nPercentage =") fp2.write( str (per)) print("\n") fp1.close() fp2.close()

Example – Illustrate various seek() options Create a file seek1.txt as shown below:

fp =open("seek1.txt","r") content = fp.read () print("Actual Content") print("--------------") print(content) print("\ nSecond Attempt") print("\n--------------") content= fp.read () print(content) print("\ nThird Attempt") print("-------------") print("\ nMoved the Cursor to the beginning of the file") print("\n--------------------------------------------") fp.seek (0,0) content= fp.read () print(content) print("\ nForth Attempt") print("-------------") print("\n set the cursor after 33 bytes from the beginning") print("---------------------------------------------------") fp.seek (33,0) content= fp.read () print(content) fp.close () Type the given below python code in editor

Python Shell Output Actual Content -------------- You are requested to assemble in the auditorium at 9.00 a.m. for Mahatma Gandhi Scholarship Award Ceremony Thanks & Regards, Office of the Principal Second Attempt -------------- Third Attempt ------------- Moved the Cursor to the beginning of the file -------------------------------------------- You are requested to assemble in the auditorium at 9.00 a.m. for Mahatma Gandhi Scholarship Award Ceremony Thanks & Regards, Office of the Principal Forth Attempt ------------- set the cursor after 33 bytes from the beginning --------------------------------------------------- the auditorium at 9.00 a.m. for Mahatma Gandhi Scholarship Award Ceremony Thanks & Regards, Office of the Principal

LET’s TRY Write a program to perform the following operations using seek() and basic file operations. Open file weekdays.txt in write mode Write weekdays from Monday to Friday in the file. Use seek() to read the content of the file. Set the pointer to the end of the file and append two remaining weekdays, i.e. Saturday and Sunday to the existing file weekdays.txt. Read and print all the content of the file.  
Tags