What i'm trying to do is writing Program that print the number of newlines,words and characters in counted file by python

I'm lost and i do not how should i do it?

that what i got so far

infilename = input("Enter the name of the file:")

infile = open ( infilename ,'r')

for line in infile:
    line = line.split()



print (line)

Recommended Answers

All 6 Replies

What is a counted file?
Please provide a testcase.

For example you can count newlines and characters like this:

infilename = input("Enter the name of the file:")
count_newlines=0
count_chars=0
with open (infilename ,'r') as infile:
    for line in infile:
        count_newlines+=1
        count_chars+=len(line)

print(count_newlines)
print(count_chars)

thank u
how can i count the bytes in file ?

sorry
how can i count the words in file ?

You split the line with space.
count_words+=len(line.split(" "))

A character usually takes up one byte.

You could also follow this approach ...

infilename = input("Enter the name of the file: ")

with open (infilename ,'r') as infile:
    data = infile.read()
    count_newlines = data.count('\n')
    count_chars = len(data)
    count_words = len(data.split())


print("Lines = {}".format(count_newlines))
print("Characters/bytes = {}".format(count_chars))
print("Words = {}".format(count_words))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.