Notes File Handling
Notes File Handling
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
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)