Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Reading a Whole File

Reading a Whole File

Dev’s port has the log’s name from the command line. Now it needs to read the log. In this lesson, you’ll read a whole file in JavaScript with one call, and find out what that call really hands back. Dev’s Python tool loops over the log line by line, but the port reads the whole text first. In Python, that’s f.read() inside a with block:

Language: Python
import sys

path = sys.argv[1]
with open(path, encoding="utf-8") as f:
    text = f.read()
print(text)
lines = text.split("\n")

Run as python digest.py logs/week-44.log, it prints the ten readings of week 44, from Mon 07:00,71.5 to Fri 13:00,67.5.

Python’s open() is built in. In JavaScript, reading files comes from a module, node:fs/promises. The node: prefix marks one of Node’s own modules, so there’s nothing to install. One call, readFile(), takes the path and "utf8", where Python passes an encoding. There’s no file object and no with block: readFile() opens the file, reads it, and closes it for you. Here’s the line-by-line port. What does it log when it runs?

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

00:02 Dev’s port has the log’s name from the command line. Now it needs to read the log. In this lesson, you’ll read a whole file in JavaScript with one call, and find out what that call really hands back.

00:18 Dev’s Python tool loops over the log line by line, but the port reads the whole text first. In Python, that’s f.read() inside a with block.

00:30 Run on week forty-four, it prints the ten readings.

00:35 Python’s open() is built in. In JavaScript, reading files comes from a module, so the port imports readFile() first. The node: prefix marks one of Node’s own modules, so there’s nothing to install.

00:52 The path line ports as before, with the first argument at index two. Then the read: one call takes the path and "utf8", where Python passes an encoding.

01:05 There’s no file object and no with block: readFile() opens the file, reads it, and closes it for you. The last two lines port line for line.

01:17 Python’s version printed the readings, and its last line ran without a problem. What does the JavaScript port log when you run it? Your turn: pause the video and predict.

01:34 It logs a pending promise, not the text. Then the split line stops the script with a TypeError: split is not a function. A promise isn’t a string, so it has no .split().

01:48 The card keeps only Node’s error line, and the readings never print.

01:55 Python’s f.read() waits for the file before the next line runs. readFile() doesn’t: reading takes time, so it hands back a promise of the text straight away, pending until the file is read.

02:09 await waits for that promise and gives you the text it holds. How promises work gets a course of its own later. For now, put await in front of the call.

02:22 Your digest.js is a module, since package.json sets its type. So await works at the top level, outside any function.

02:33 The script still reads top to bottom, as Python’s does.

02:39 Run it again on week forty-four, and it logs the ten readings, as Python did. This time the last line runs without an error.

02:49 Watch out for the second argument: without "utf8", readFile() gives a buffer of bytes, as Python’s open() does only in "rb" mode, and split fails the same way, so always pass it for a text file.

03:04 Now run both versions on week forty-five, whose log Dev hasn’t copied yet. Python stops with a FileNotFoundError. JavaScript stops too, with an error whose code is ENOENT, short for error, no entry, the same error Python shows as Errno 2.

03:25 Your terminal shows more lines around it, but the error line and its code are what matter. Handling that error comes in a later lesson.

03:35 At the top level, await is all it takes. When the read moves into a function in readings.js, so Asha can reuse it, it takes one more word.

03:47 await inside a function needs async in front of the word function. Even though readLog() returns the text, calling it hands back a promise of that text, like readFile(), so whoever calls it awaits it in turn.

04:02 Take it as a recipe for now.

04:06 That’s the JavaScript way: one awaited call reads the whole file, with "utf8" for text. There’s no file object to close, and the script still reads top to bottom.

04:19 Next up is Splitting a File Into Lines, a text lesson. Python’s most common file loop takes the readings one line at a time, and you’ll port that loop.

Become a Member to join the conversation.