We use Python 3.6

go back Level: Beginner (img: BG / score: 2) level Bite 96. Build Unix' wc program in Python

26 out of 26 users completed this Bite ...
Will you be Pythonista #27 to crack this Bite?
Average Bite difficulty 1-10 rating: 1.0
» You can do it! 😌

In this Bite you will convert Unix' wc command into Python. Your function takes a file (absolute path), reads it in and calculates the lines/words/chars. It returns a string of these numbers and the filename, like as a typical wc output, for example:

$ wc wc.py
      13      56     514 wc.py

Don't worry about the amount of white space between the columns, you can use tabs or spaces.

Unix files add an extra newline to the end, you don't have to make that assumption here, so 'Hello\nworld' == 11 chars not 12 as Unix' wc would return. Let's keep it simple. Do note that newline (\n) counts as a char.

See the tests for more info. We used tempfile.NamedTemporaryFile to work with temp files, pretty useful module.

Have fun and keep coding in Python!

Github login button