Problem Statement:
Create a text file and add course outcomes
of this course. Implement file operations on
it.
Algorithm:
1.Initial File Writing:
○ Open the file course_outcomes in write mode
("w").
○ Write the string "Course Outcomes\n" into the
file.
○ Close the file.
2.File Reading:
○ Open the file course_outcomes in read mode
("r").
○ Read and print the entire content of the file.
○ Close the file.
3.File Overwriting with Introductory Text:
○ Open the file course_outcomes again in write
mode ("w").
○ Write the string "The Course Outcomes are:
\n" into the file.
○ Close the file.
4.Reading and Printing the New Content:
○ Open the file course_outcomes in read mode
("r").
○ Read and print the first line of the file (which is now
"The Course Outcomes are: \n").
○ Close the file.
5.Appended Writing Based on User Input:
○ Open the file course_outcomes in append mode
("a").
○ Ask the user for the number of subjects (n).
○ For each subject (loop from i = 0 to n-1):
■ Prompt the user to enter the name of the subject.
■ Ask for the number of course outcomes (co) for
that subject.
■ For each course outcome (loop from i = 0 to
co-1):
■ Prompt the user to enter the course outcome
description.
■ Write the outcome to the file in the format:
"Outcome Number <i+1> <outcome>\n".
○ Close the file.
6.Final File Reading:
○ Open the file course_outcomes in read mode
("r").
○ Read and print the entire content of the file, which
now includes the introductory text and the course
outcomes for each subject.
○ Close the file.
Flowchart:
Program Source Code:
f=open("course_outcomes","w")
f.write("Course Outcomes\n")
f.close()
f=open("course_outcomes","r")
print(f.read())
f.close()
#========================================
f=open("course_outcomes","w")
f.write("The Course Outcomes are: \n")
f.close()
f=open("course_outcomes","r")
print(f.readline())
#========================================
f=open("course_outcomes","a")
print("Enter the number of subjects")
n=int(input())
#co=int(input("enter the no. of course outcomes: "))
for i in range(n):
name=input("enter the name of the subject: ")
co=int(input("enter the no. of course outcomes: "))
print("The subject",name,"has the following outcomes:")
for i in range(co):
outcome=input("enter the course outcome: ")
f.writelines([str(i+1)," ",outcome,"\n"])
f.close()
f=open("course_outcomes","r")
print(f.read())
Sample output:
Course Outcomes
The Course Outcomes are:
Enter the number of subjects
1
enter the name of the subject: psp
enter the no. of course outcomes: 2
The subject psp has the following outcomes:
enter the course outcome: Execute Python programs
enter the course outcome: Test Python programs for various
inputs
The Course Outcomes are:
1 Execute Python programs
2 Test Python programs for various inputs