0% found this document useful (0 votes)
84 views4 pages

Python File Handling and Word Count

The document describes a Python program to perform various file handling operations: 1) It copies the contents of one text file to another, and prints the word count and longest word of the original file. 2) It counts the number of words in a file given as a command line argument. 3) It finds the frequency of words in a text file, and prints the words and their counts.

Uploaded by

sarguruxerox
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)
84 views4 pages

Python File Handling and Word Count

The document describes a Python program to perform various file handling operations: 1) It copies the contents of one text file to another, and prints the word count and longest word of the original file. 2) It counts the number of words in a file given as a command line argument. 3) It finds the frequency of words in a text file, and prints the words and their counts.

Uploaded by

sarguruxerox
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

EX NO: 8 FILE HANDLING

DATE: a) COPYING, WORD COUNT & LONGEST WORD


AIM:
To write a python program to perform various operations on File.
ALGORITHM:
Step1: Start the program
Step2: Create a file, and save as [Link] in notepad.
Step3: Open the file [Link] in read mode.
Step3: Open a new file [Link] in write mode.
Step4: Read the contents from [Link] file using read function.
Step5: Write the contents to [Link] using write function.
Step6: Display the word count ([Link])
Step7: And display the longest word in the file ([Link])
Step8: Stop the program.
PROGRAM:
f1=open("[Link]","r")
f2=open("[Link]","w")
str=[Link]()
[Link](str)
print("content of file([Link]) is copied to file([Link])")
[Link]()
[Link]()
print("\nThe contents of file([Link]) is:")
c=open("[Link]","r")
print([Link]())
[Link]()
f3=open("[Link]","r")
words=[Link]().split()
print("The word count in the file([Link]) is:",len(words))
max_len=len(max(words,key=len))
for word in words:
if len(word)==max_len:
print("The longest word in the file([Link]) is:",word)
[Link]()

OUTPUT:
content of file([Link]) is copied to file([Link])

The contents of file([Link]) is:


Hello
This is python programming lab.

The word count in the file([Link]) is: 6


The longest word in the file([Link]) is: programming

RESULT:
Thus, the above program was executed successfully and the output was verified.
EX. NO: 8 FILE HANDLING
DATE: b) COMMAND LINE ARGUMENTS (WORD COUNT)
AIM:
To write a Python program to count the words in a file given in command line arguments.
ALGORITHM:
Step 1: Start the program.
Step 2: Open the file given in the command line arguments in read mode.
Step 3: Read the contents of the file.
Step 4: Count the number of words in the file.
Step 5: Display the number of words.
Step 6: Stop the program.
PROGRAM:
import sys
def countWords(sentence):
wordCount = 0
data = [Link]('\\n')
for line in data:
line = [Link]().split(' ')
line = [[Link](',.;!?') for word in line]
wordCount += len(line)
return wordCount
def main():
if len([Link]) == 2:
f = open([Link][1],'r')
sentence = [Link]()
count = countWords(sentence)
print("Number of words in the given sentence:",count)
else:
print("Unexpected number of command line arguments!")
[Link]()
if __name__=='__main__':
main()

[Link]
I LOVE PYTHON PROGRAMMING TO THE CORE

OUTPUT:
C:\Users\olgar\AppData\Local\Programs\Python\Python311\Scripts>py [Link] [Link]
Number of words in the given sentence: 7

RESULT:
Thus, the above program was executed successfully and the output was verified.
EX. NO: 8 FILE HANDLING
DATE: c) FINDING MOST FREQUENT WORDS
AIM:
To write a Python program to find the frequency of words in a text file.
ALGORITHM:
Step 1: Start the program.
Step 2: Read the filename.
Step 3: Open the file in read mode.
Step 4: Read each line from the file and perform following operations on it.
Step 4.1: Change it to lowercase.
Step 4.2: Remove the punctuations.
Step 4.3: Split each line into words and count them.
Step 5: Print the words and it count.
Step 6: Stop the program.
PROGRAM:
def main():
filename=input("enter the file: ").strip()
infile=open(filename,"r")
wordcounts={}
for line in infile:
processLine([Link](),wordcounts)
pairs=list([Link]())
items=[[x,y] for (y,x) in pairs]
[Link]()
for i in range(0, len(items), 1):
print(items[i][1]+"\t"+str(items[i][0]))
def processLine(line,wordcounts):
line=replacePunctuations(line)
words=[Link]()
for word in words:
if word in wordcounts:
wordcounts[word]+=1
else:
wordcounts[word]=1
def replacePunctuations(line):
for ch in line:
if ch in "~@#$%^&*()_-+=<>?/.,:;!{}[]''":
line=[Link](ch," ")
return line
main()
[Link]
A file is a collection of data stored on a secondary storage device like hard disk. They can be
easily retrieved when required.
Python supports two types of files:
They are Text files & Binary files.

OUTPUT:
enter the file:[Link]
are 1
be 1
binary 1
can 1
collection 1
data 1
device 1
disk 1
easily 1
file 1
hard 1
is 1
like 1
on 1
python 1
required 1
retrieved 1
secondary 1
storage 1
stored 1
supports 1
text 1
two 1
types 1
when 1
of 2
they 2
a 3
files 3

RESULT:
Thus, the above program was executed successfully and the output was verified.

You might also like