0% found this document useful (0 votes)
2K views13 pages

CS PROJECT FILE Class 12

The document discusses text file handling in Python. It defines text files as files containing human-readable characters that can be opened by any text editor. It describes opening, writing to, reading from, and closing text files using various Python functions and methods like open(), write(), readline(), and close(). It also provides examples of writing and reading strings to/from a text file, and 10 questions with solutions for practicing common text file handling tasks in Python.

Uploaded by

Vishal
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)
2K views13 pages

CS PROJECT FILE Class 12

The document discusses text file handling in Python. It defines text files as files containing human-readable characters that can be opened by any text editor. It describes opening, writing to, reading from, and closing text files using various Python functions and methods like open(), write(), readline(), and close(). It also provides examples of writing and reading strings to/from a text file, and 10 questions with solutions for practicing common text file handling tasks in Python.

Uploaded by

Vishal
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/ 13

CS PROJECT FILE : TEXT FILE HANDLING

INTRODUCTION TO TEXT FILES

● A File is a named location on a secondary storage media where data are


permanently stored for later access.
● Every file is a series of bytes.
● There are mainly 2 types of data files:-
1. TEXT FILE
2. BINARY FILE
● A text file consists of human readable characters which can be opened by
any text editor.
● A binary file is made up of non-human readable characters and symbols ,
which require specific programs to access its contents.

TEXT FILE HANDLING

● A text file can be understood as a sequence of :-


1. Alphabets
2. Numbers
3. Other special symbols
● Examples of text file:
1. .txt
2. .py
3. .csv
● Each line of a text file is terminated by a special character ,called End Of
Line(EOL).
● Example : The default EOL character in Python is the newline(\n).
● When a text editor or a program interpreter encounters the ASCII
equivalent of the EOL character ,it displays the remaining file contents
starting from a new line.

OPENING A TEXT FILE

● To open a file in Python, we use the open() function.


● The syntax is as follows:-
● file_object=open("textfilename.txt","access_mode")
● file_object is the variable which stores the file handle.
● If the file does not exist,the above statement creates a new file (in append
or write mode only).
● <file.closed> returns True if the file is closed and False otherwise.
● <file.mode> returns the access mode in which the file was opened.
● <file.name> returns the name of the file.
● Textfilename is the name of the file which is to be opened.
● The access mode is an optional argument that represents the mode of
opening of the file.
● By Default: “r” or read mode
● Other modes are as follows:-
1. r= reading
2. w=writing
3. a=appending
4. r+ or +r=read and write mode
5. a+ or +a=append and read mode

CLOSING A TEXT FILE

● While closing a file,the system frees the memory allocated to it.


● The syntax is as follow:
● File_object.close()
● File_object is the object that was returned while opening the file.
● Python makes sure that any unwritten or unsaved data is flushed off
( written) to the file before it is closed.
● If the file object is re-assigned to some other file,the previous file is
automatically closed.

OPENING A FILE WITH A CLAUSE

● The syntax is as follows:


● with open(file_name,access_mode) as file_object:
● Any file that is opened using this clause is closed automatically.

WRITING TO A TEXT FILE

● We first need to open it in write or append mode.


● If we open an existing file in write mode, the previous data will be erased,
and the file object will be positioned at the beginning of the file.
● If we open the file in append mode, new data will be added at the end of the
previous data as the file object is at the end of the file.
● write() = for writing a single string
● writeline()=for writing a sequence ofstrings.

THE WRITE METHOD

● It takes a string as an argument and writes it to the text file.


● We need to add a newline character (\n) at the end of each line to mark the
end of line.
● The syntax is as follows:
● File_object.write(“hello\n”)
● \n is taken as single character.
● If numeric data is to be written to a text file,the data need to be converted
into string before writing to the file.
● When the close() is executed ,the contents are moved to the file located on
the permanent storage.

THE WRITELINES() METHOD

● It is used to write multiple strings to a file.


● We pass iterable objects like lists,tuples,etc containing strings to the
writelines() method.
● It does not return the number of characters written in the file.

THE READ() METHOD

● It is used to read a specified number of bytes of data from a data file.


● If no argument or a negative number is specified ,the entire file is read.

THE READLINE([N]) METHOD

● 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.

THE READLINES() METHOD

● 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)

THE TELL() METHOD

● 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()

THE SEEK() METHOD

● It is used to position the file object at a particular position in a file.


● The syntax is given below:-
● File_object.seek(offset[,referenced point])
● Offset is the number of bytes by which the file object is to be moved.
● Referenced point indicates the starting position of the file object.
● The referenced point can have the following values:-
1. 0=beginning of the file
2. 1=current position of the file
3. 2=end of file
● By default=> referenced point=0

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”

“It is a fun place”

“ You will learn and play”

CODING:

f=open("hello.txt","w")

a=["welcome my class\n","It is a fun place\n","You will learn and play\n"]

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")

ans=input("do you want to continue? END/START")

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():

if i[0].islower()==True and i[0] in 'aeiou':

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

print("total no.of spaces are=>",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():

if i[0].islower()==True and i[0] in 'aeiou':

g.write(i)

f.close()

g.close()

OUTPUT:

WORK COMPLETED
INDEX

TOPIC PAGE

INTRODUCTION TO TEXT FILE 1

TEXT FILE HANDLING 1

OPENING A TEXT FILE 1-2

CLOSING A TEXT FILE 2

OPENING A FILE WITH A CLAUSE 2

WRITING TO A TEXT FILE 2

THE WRITE() METHOD 2-3

THE WRITELINES() METHOD 3

THE READ() METHOD 3

THE READLINE(N) METHOD 3

THE READLINES() METHOD 3

THE TELL() METHOD 3

THE SEEK() METHOD 3-4

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

You might also like