The problem for this week consisted of defining a function that opens a .txt file and counts the number of lines and characters in it 😀 .
To do it, I first researched about how to read and write files in python, and I found this very useful page:
https://www.digitalocean.com/community/tutorials/how-to-handle-plain-text-files-in-python-3
I then proceeded to start writing my code, but when I was almost done I faced a small problem, which was that it was counting the enters between lines as characters. I remembered that my teacher Ken had told us the day before about a function that eliminates additional spaces, but I couldn’t recall its name. I asked a friend from that class about it, and he reminded me of it: it is .strip()
I used said function, and lastly my program worked as it should ❤ :
The text file I used was this:
And the program ran like this:
2 thoughts on “Files!”