Files!

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 ❤ :files

The text file I used was this:

texto

And the program ran like this:files_run

2 thoughts on “Files!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s