Skip to content

File I/O

Read and write files using context managers.

with open("data.txt", "w") as f:
f.write("hello")
with open("data.txt") as f:
print(f.read())

C# uses using with StreamWriter; JavaScript relies on the fs module.

  1. Which Python statement automatically closes a file?
    Answer`with`.
  2. Name the Node.js module for file operations.
    Answer`fs`.