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)

Edited 3 Years Ago by goo_1

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:")
with open (infilename ,'r') as infile:
    for line in infile:


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))
This question has already been answered. Start a new discussion instead.