Python File Programs
top of page

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



37 views1 comment

Recent Posts

See All

Python program to calculate the factorial of a number

Write a Python function to calculate the factorial of a number (a non-negative integer). The function accepts the number as an argument. This program is most for O Level Practical Exam so read it care

NumPy program to find the most frequent value in the list.

This program is most for O Level Practical Exam so read it carefully. Program: import numpy as np x = np.array([1,2,3,4,5,7,2,1,1,1,8,9,1]) print(x) print("most frequent value in the list:") print(np.

bottom of page