top of page
Writer's pictureRajesh Singh

Python File Programs



Write a program to create a text file of students to store Roll No., Name and three subject marks obtained by the students in a class of 15 students.

Program:

student=open("marks.txt",'w')

for i in range(3):

record=""

r=input("Enter Roll No. :")

record=record+r+"\n"

nm=input("Enter Name:")

record=record+nm+"\n"

m1=input("Enter Marks1 :")

record=record+m1+"\n"

m2=input("Enter Marks 2:")

record=record+m2+"\n"

m3=input("Enter Marks 3:")

record=record+m3+"\n"

student.write(record)

student.close()


Write a program to add details of two more student into the text file 'marks.txt' which is already created on the disk.

Program:

student=open('marks.txt', 'a')

for i in range (2):

record=""

r=input("Enter Roll No. :")

record=record+r+"\n"

nm=input("Enter Name: ")

record=record+nm+"\n"

m1=input("Enter Marks 1: ")

record=record+m1+"\n"

m2=input("Enter Marks 2:")

record=record+m2+"\n"

m3=input("Enter Marks 3: ")

record=record+m3+"\n"

student.write(record)

student.close()

Output:

Enter Roll No. :101

Enter Name: Raj

Enter Marks 1: 89

Enter Marks 2:90

Enter Marks 3: 78

Enter Roll No. :102

Enter Name: Rita

Enter Marks 1: 89

Enter Marks 2:67

Enter Marks 3: 78


Write a python program to read a random line from a file

Program:

import random


def random_line(afile):

line = next(afile)

for num, aline in enumerate(afile, 2):

if random.randrange(num):

continue

line = aline

return line



40 views1 comment

Recent Posts

See All

1 Comment


ankitkumartkd95
Feb 13, 2021

Best python file programs for O Level. Thanks for post.

Like
bottom of page