CS PROJECT FILE Class 12
CS PROJECT FILE Class 12
● It reads one complete line from a file where each line terminates with a
newline(\n) character.
● If no argument or a negative number is specified ,it reads a complete line
and returns string.
● We use looping/iterating over a file object to read the entire file line by
line.
● It reads all the lines and returns the lines along with newline as a list of
strings.
● Lines in the file become members of a list , where each list element ends
with a newline character(\n)
● It returns an integer that specifies the current position of the file object in
the file.
● The position specified is the byte position from the beginning of the file till
the current position of the file object.
● The syntax is as follows:-
● File_object.tell()
QUESTIONS
Q-1: Write a command(s) to write the following lines to the text file named
“hello.txt”. Assume that the file is opened in append mode .
“Welcome my class”
CODING:
f=open("hello.txt","w")
f.writelines(a)
f.close()
OUTPUT:
Q-2: Write a python program to open the file “hello.txt” used in Q-1 in read mode
to display its contents. What will be the difference if the file was opened in write
mode instead of append mode?
CODING:
f=open("hello.txt")
for i in f.readlines():
print(i)
f.close()
OUTPUT:
If the file is opened in write mode then all the previous existing data of the file
would be erased and the new data specified will be inserted into the file. The
append mode adds the specified data to the file without erasing the previous
existing data.
Q-3: Write a program to accept string/sentences from the user till the user enters
“END” to. Save the data in a text file and then display only those sentences which
begin with an uppercase alphabet.
CODING:
f=open("hello.txt","w")
while True:
a=input("enter sentence")
f.write(a)
f.write("\n")
if ans=="END":
break
f.close()
f=open("hello.txt")
for i in f.readlines():
if i[0].isupper()==True:
print(i)
f.close()
OUTPUT:
Q-4: Write a program in python to read entire content of the file “hello.txt”.
CODING:
f=open("hello.txt")
for i in f.readlines():
print(i)
f.close()
OUTPUT:
Q-5: Write a program in python to display last two characters of all the lines from
the file “hello.txt”.
CODING:
f=open("hello.txt")
for i in f.readlines():
print(i[-3],i[-2])
f.close()
OUTPUT:
Q-6: Read a file “hello.txt” and copy contents of your file to another text file after
removing the character ‘e’.
Content of hello.txt=>
f=open("hello.txt")
g=open("cake.txt","w")
for i in f.read():
if i!='e':
g.write(i)
f.close()
g.close()
OUTPUT:
Q-7: Read a file “hello.txt” and copy all the lines starting with lowercase vowels to
another file “happy.txt”.
Content of hello.txt=>
CODING:
f=open("hello.txt")
g=open("happy.txt","w")
for i in f.readlines():
g.write(i)
f.close()
g.close()
OUTPUT:
Q-8: Read a text file “hello.txt” and display all the words ending with letter ‘a’ in
newline on screen.
Contents of hello.txt=>
CODING:
f=open("hello.txt")
for i in f.read().split():
if i[-1]=='a':
print(i)
f.close()
OUTPUT:
Q-9:Read a text file “hello.txt” and display the total number of times spaces
present in the file.
Content of hello.txt=>
CODING:
f=open("hello.txt")
c=0
for i in f.read().split():
c+=1
f.close()
OUTPUT:
Q-10:Read a file “hello.txt” and copy all the lines starting with lowercase vowels to
another file “happy.txt”
Content of hello.txt=>
CODING:
f=open("hello.txt")
g=open("happy.txt","w")
for i in f.readlines():
g.write(i)
f.close()
g.close()
OUTPUT:
WORK COMPLETED
INDEX
TOPIC PAGE
QUESTIONS 4-11
CERTIFICATE
THIS IS TO CERTIFY THAT , SNIGDHA
SHARMA, STUDENT OF CLASS XII A-2,ROLL
NO.12233 , SESSION (2022-23), HAS
SUCCESSFULLY COMPLETED HER
COMPUTER SCIENCE PROJECT ON “TEXT
FILE HANDLING” TOPIC UNDER THE
GUIDANCE OF Ms. VINITA SHARMA