0% found this document useful (0 votes)
37 views12 pages

CS Proiject

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views12 pages

CS Proiject

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

AIM

A text file “text_report.txt” exists on a disk with the


following content.
Python is an object oriented language.
it is a "dynamically typed" language.
You do not need to declare variables before using them.or declare their type.
every variable in Python is an object.
python supports two types of numbers-integers and floating point numbers.
Python is simple in syntax and understanding.

Write a function capitalize_Sentence() to create a copy


of file "test_report.txt" named as "file1.txt", which
should convert the first letter of the file and the first
alphabetic character following a full stop into upper
case.

DOCUMENTATION
 NAME OF THE SOURCE FILE: 4.22 PI-14

 SIZE OF THE FILE: 4.00KB

 VERSION OF PYTHON USED: Python 3.11.4

 MODULE OF PYTHON USED: IDLE


 FUNCTIONS IN PYTHON USED:
print(),file.readline(),line.rstrip(),file.write(),
open(“<file name>.txt”,”<mode>”)

ALGORITHM
 Define the function capitalize_Sentence.
 Attempt the following steps, catching any file-
related errors (e.g., if the file does not exist):
 Open the source file "test report.txt" in
read mode.
 Open the target file "file1.txt" in write mode.
 Repeat the following steps for each line in the
source file:
 Read a line from the source file.
 If the line is empty (end of file), exit the loop.
 Remove trailing whitespace from the line.
 Initialize an empty string capitalized_str to
store the capitalized sentence.
 Add the uppercase of the first character in the line
to capitalized_str (if the line has content).
 For each character in the line starting from the
second character:
 If the character is a period ("."), add it to
capitalized_str.
o Increment the index and check if there’s

another character in the line.


o If so, capitalize this character and add it to

capitalized_str.
 Otherwise, add the character to
capitalized_str as it is.
 Write the capitalized sentence to the target file,
followed by a newline.
 Close both the source and target files.
 If a FileNotFoundError occurs, print an error
message indicating the source file does not exist.
 Call the function capitalize_Sentence.
CODE
def capitalize_Sentence():
f1= open("test report.txt","r")
f2=open("filel.txt", "w")
while 1:
line fl.readline()
if not line:
break
line= line.rstrip()
lineLength=len (line)
str =’’
str=str + line[0].upper()
i=1
while i<lineLength:
if line[i]==".":
str=str + line[i]
i=i+1
if i >= lineLength:
break
str=str + line [i].upper()
else:
str=str + line [i]
i=i+1
f2.write (str)
else:
print("Source file does not exist")
f1.close()
f2.close()
capitalize_Sentence ()

OUTPUT
Python is an object oriented language.
It is a "dynamically typed" language.
You do not need to declare variables before using them.or declare their type.
every variable in Python is an object.
python supports two types of numbers-integers and floating point numbers.
Python is simple in syntax and understanding.
AIM
Write a program to insert /append a record in the
binary file “student.dat”

DOCUMENTATION

 NAME OF THE SOURCE FILE: 4.26 PI-19

 SIZE OF THE FILE: 3.00KB

 VERSION OF PYTHON USED: Python 3.11.4

 MODULE OF PYTHON USED: IDLE

 FUNCTIONS IN PYTHON USED: print(),


str.upper,list.append,pickle.dump and
f=open(“<filename>”,”<mode>”)
ALGORITHM
1.Start the program
2. Initialize an empty list called record to store student
information.
3. Repeat the following steps until the user decides to
stop:
 Prompt the user to enter the following details:
o roll_no: Roll number of the student (integer).

o name: Name of the student (string).

o marks: Marks obtained by the student

(integer).
 Store the entered data as a list: [roll_no, name,
marks].
 Append this list to record.
 Ask the user if they wish to enter more records.
 If the user enters 'N' (no), exit the loop. Otherwise,
continue.
3.Open a file named "student" in write-binary ("wb")
mode.
4. Save the record list to the file using pickle.dump().
5. Print a message indicating that the record has been
added.
6.Close the file.
CODE
import pickle
record= []
while true:
roll_no=int (input("Enter student Roll no :"))
name=input ("Enter the student Name:")
marks=int (input ("Enter the marks obtained :"))
data=[roll_no, name, marks]
record.append (data)
choice=input("Wish to enter more records (Y/N)?: ")
if choice.upper() -- 'N':
break
f=open("student", "wb")
pickle.dump(record, f)
print("Record Added")
f.close()
OUTPUT
Enter student Roll no :2
Enter the student Name: Radhika
Enter the marks obtained : 490
Wish to enter more records (Y/N)?: y
Enter student Roll no :3
Enter the student Name: Shaurya
Enter the marks obtained : 450
Wish to enter more records (Y/N)?: n
Record Added
AIM
Write a program to count the number of records
present in “student.csv” file
Student.csv file contains ;-
['Name', 'Class', 'Marks']
['Rinku', 'XII', ' 90 deg ]
['Veenu', ^ r X * I' , ' 88' ]
['Ajay', ' X * I' , ' 78 deg * 1
['Sushant', x' , ' 70 deg * 1
['Radhika', ' T * X' , '80']
['Shaurya', overline ^ prime X' , ' 90 deg
['Deepak', X * I' , ' 60 deg ]
['Jatin', 'XII', '70']
['Himesh', ^ prime XI * I' , '78']
['Vinay', ' X * I' , '99']
>>>

DOCUMENTATION

 NAME OF THE SOURCE FILE: 4.37 PI-26

 SIZE OF THE FILE: 2.00KB

 VERSION OF PYTHON USED: Python 3.11.4


 MODULE OF PYTHON USED: IDLE

FUNCTIONS IN PYTHON USED:


print(),f=open(“<file>.csv”,’mode’)

ALGORITHM
 Import the csv module to handle CSV file
operations.
 Open the file "student.csv" in read mode
('r'), and assign it to the variable f.
 Create a CSV reader object csv_reader using
csv.reader(f) to read the contents of the file.
 Initialize a counter variable c to 0. This will keep
track of the number of records.
 For each row in csv_reader:
 Increment the counter c by 1.
 Print the total number of records using the value of
c.
 Close the file f.
CODE
import csv
f=open(“student.csv”,’r’)
csv_reader=csv_reader(f)
c=0
for row in csv_reader:
c=c+1
print(“No. of records are:”,c)
f.close()

OUTPUT
No of records are: 11

You might also like