python file count words code example

Example 1: pytho count avro file

Get the latest released version or build it from source.
https://github.com/jwoschitz/avrocount/releases/latest

Then simply invoke the tool via

java -jar avrocount.jar /path/to/myfile.avro

And it will print the amount of records found within the file to stdout.
  
Folders containing avro files

java -jar avrocount.jar /path/to/folder

Example 2: Python program that takes a text file as input and returns the number of words of a given text file

with open('file1.txt','r+') as f:
	lines=f.read()
	words=lines.split()
	print(len(words))

Example 3: Python program that takes a text file as input and returns the number of words of a given text file

with open('file1.txt','r+') as f:
	numw=0
	for line in f:
		words= line.split()
		print(words)
		numw += len(words)
	print(numw)

Example 4: Python Program to Count the Occurrences of a Word in a Text File

#get file object reference to the file
file = open("C:\workspace\python\data.txt", "r")
#read content of file to string
data = file.read()
#get number of occurrences of the substring in the string
occurrences = data.count("python")
print('Number of occurrences of the word :', occurrences)

Tags:

Java Example