-1

I've tried to run it, but it doesn't seem to read the file. I'm new to coding and I tried looking for help on google to no avail. Please can someone explain to me what i did wrong.

name = input('Enter your file name here: ')
handle = open(name, 'r')
print(handle.read())
counts = dict()
    for line in handle.read():
        words = line.strip()
    for word in words:
        counts[word] = counts.get(word, 0) + 1

Bigcount = None
Bigword = None
    for word,count in counts.items():
        if Bigcount is None or count < Bigcount:
        Bigcount = word
        Bigword = count

print(Bigword, Bigcount)
tripleee
  • 158,107
  • 27
  • 234
  • 292
  • When you say "it doesn't read the file" do you mean `print(handle.read())` does not print anything? – TessellatingHeckler May 28 '22 at 19:02
  • When you open and `read()` you have exhausted the file; further attempts to `read()` will return nothing. Instead, save the result of the first `read()` into a variable, then manipulate that variable. – tripleee May 28 '22 at 19:38
  • Similarly `for line in handle.read():` will return the entire file in `line` and loop only once. If you really want to read a line at a time, simply use `for line in handle:` (though perhaps notice also that the value of `line` will include the `"\n"` at the end of every line which has one). – tripleee May 28 '22 at 19:43
  • Thank you very much for your response... it was very useful. I tried it out and it worked perfectly well. – Enow Cha May 30 '22 at 08:47

0 Answers0