Splitting a File Into Lines

Your port can already read the week’s log. await readFile("logs/week-44.log", "utf8") returns the whole file as text. The Python tool takes its readings one at a time from for line in f:, Python’s most common file loop, which is the next part to port.

Count the Lines in the Week’s Log

First, check that the loop sees Dev’s ten readings. A Python counter prints 10:

Language: Python
count = 0
with open("logs/week-44.log", encoding="utf-8") as f:
    for line in f:
        count += 1
print(count)

Like Python’s Path(path).read_text(), readFile() opens, reads, and closes the file in one call, so the port needs no with block. A scratch script next to digest.js ports the counter line by line:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.