0% found this document useful (0 votes)
11 views

Notes File Handling

Uploaded by

poojaakarthikk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Notes File Handling

Uploaded by

poojaakarthikk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

DEFINING FUNCTIONS

TO
READ DATA FROM A TEXT FILE

JINI N K
ENJOY LEARNING COMPUTER SCIENCE WITH JINI
To read character by character in a Text file

Character checking

Use read() function


→ S=f.read()
Run a loop on S
→ for i in S:
ENJOY LEARNING COMPUTER SCIENCE WITH JINI
To read character by character in Text file
f.read()
def functionname():
Name of the function as
f=open(“file.txt”) given in the question
To read as string
S=f.read()
count=0
To count characters
for i in S:
To check/read
character by character
if (condition):
count+=1
print(count)
To print count
f.close() characters

ENJOY LEARNING COMPUTER SCIENCE WITH JINI


To read words in Text file

Word-by-word checking
Use read() function
→ S=f.read()
Use split() function
→ S.split()
Run a loop on S
→ for i in S:
ENJOY LEARNING COMPUTER SCIENCE WITH JINI
To check word by word in Text file
def functionname():
Name of the function as
f=open(“file.txt”) given in the question
To read as string
S=f.read()
W=S.split() To split into words
count=0
To check/read words To count characters
for i in S:
if (condition):
count+=1
To print count
print(count) words
f.close()
ENJOY LEARNING COMPUTER SCIENCE WITH JINI
To read lines in Text file
Line-by-line checking
Use readlines() function
→ L=f.readlines()
Run a loop on L
→ for i in L:

OR
ENJOY LEARNING COMPUTER SCIENCE WITH JINI
To read lines in Text file
Line-by-line checking
Run a loop on f
→ for i in f:
if(condition)

ENJOY LEARNING COMPUTER SCIENCE WITH JINI


To check line by line in Text file
def functionname():
Name of the function as
f=open(“file.txt”) given in the question
To read as List L=f.readlines()
count=0
for i in L: To count characters
To check/read lines if (condition):
count+=1
print(count)
To print count
f.close() words

ENJOY LEARNING COMPUTER SCIENCE WITH JINI


To check line by line in Text file
def functionname():
f=open(“file.txt”)
for i in f:
if (condition):
count+=1
print(count)
f.close()

ENJOY LEARNING COMPUTER SCIENCE WITH JINI


JINI N K
ENJOY LEARNING COMPUTER SCIENCE WITH JINI

You might also like