Programming with Python

⌘K
  1. Home
  2. Docs
  3. Programming with Python
  4. Files, Modules and Packag...
  5. Illustrative Programs – Files, Modules, Package

Illustrative Programs – Files, Modules, Package

Word count

import sys
file=open(sys.argv[1],"r+")
wordcount={}
for word in file.read().split():
    if word not in wordcount:
        wordcount[word] = 1
    else:
        wordcount[word] += 1
file.close()
print ('Words in the File' , 'Count')
for key in wordcount.keys():
    print (key , wordcount[key])

Output

Copy file

sf = open("ipfile.txt","r")
df = open("opfile.txt","w")
while(True):
	nl = sf.readline()
	if nl == "":
		break
	df.write(nl)
print("File Copied")
sf.close()
df.close()

Output

Views: 0

Articles

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments