0% found this document useful (0 votes)
24 views5 pages

Objectives:: 10.3 Take Any TXT File and Count Word Frequencies in A File. (Hint: File Handling+basics)

This document discusses counting word frequencies in a text file using Python. It begins with an introduction and objectives. It then provides a theory section on file handling in Python, including how to open, read, and close files. It describes using the open() function to open a file in different modes like read ("r") and append ("a"). It also demonstrates how to close a file using the close() method. The document concludes with a sample Python program that opens a text file, splits each line into a list, counts the frequency of each word, and prints the results.

Uploaded by

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

Objectives:: 10.3 Take Any TXT File and Count Word Frequencies in A File. (Hint: File Handling+basics)

This document discusses counting word frequencies in a text file using Python. It begins with an introduction and objectives. It then provides a theory section on file handling in Python, including how to open, read, and close files. It describes using the open() function to open a file in different modes like read ("r") and append ("a"). It also demonstrates how to close a file using the close() method. The document concludes with a sample Python program that opens a text file, splits each line into a list, counts the frequency of each word, and prints the results.

Uploaded by

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

10.3 Take any txt file and count word frequencies in a file.

(hint: file handling+basics)

Objectives:
1. To learn about python as scripting option.

Theory:
File handling:
A file is some information or data which stays in the computer storage devices. You already
know about different kinds of file , like your music files, video files, text files. Python gives you
easy ways to manipulate these files. Generally we divide files in two categories, text file and
binary file. Text files are simple text where as the binary files contain binary data which is only
readable by computer.
File opening:

To open a file we use open() function. It requires two arguments, first the file path or file name,
second which mode it should open. Modes are like

 “r” -> open read only, you can read the file but can not edit / delete anything inside
 “w” -> open with write power, means if the file exists then delete all content and open it
to write
 “a” -> open in append mode
The default mode is read only, ie if you do not provide any mode it will open the file as
read only. Let us open a file
>>> fobj = open("love.txt")

>>> fobj

<_io.TextIOWrapper name='love.txt' mode='r' encoding='UTF-8'>

Closing a file:

After opening a file one should always close the opened file. We use method close() for this.
>>> fobj = open("love.txt")

>>> fobj

<_io.TextIOWrapper name='love.txt' mode='r' encoding='UTF-8'>

>>> fobj.close()

Reading a file:

To read the whole file at once use the read() method.


>>> fobj = open("sample.txt")

>>> fobj.read()

'I love Python\nPradeepto loves KDE\nSankarshan loves Openoffice\n'

Program:
f=open("10cfile.txt")
d={}
for line in f:
l=line.split()
#print(line)
#print(l)

for word in l:
#print(word)
if word in d:
d[word]=d[word]+1
else:
d[word]=1
for key in d:
print key," : ",d[key]
Output:
it@it-OptiPlex-3046:~$ python uox10.py
a: 2
A: 1
Peter : 4
of : 4
Piper : 4
pickled : 4
Where's : 1
picked : 4
peppers : 4
the : 1
peck : 4
If : 1
Conclusion:
File handling and manipulation of data using list and dicitionary learnt.
5

You might also like