-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08_files.py
More file actions
31 lines (20 loc) · 971 Bytes
/
08_files.py
File metadata and controls
31 lines (20 loc) · 971 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""Exercise 08 — Files.
Tutorial: https://pythonbeginner.help/learn/python-file-handling-basics-read-and-write/
TODO:
1. Write the lines in `lines` to a temp file called `greeting.txt`.
2. Read the same file back into `contents` (as a single string).
The file should live in the current working directory and is cleaned up
automatically after the self-check runs.
Run: python exercises/08_files.py
"""
import os
lines = ["Hello, world!", "Welcome to Python.", "Goodbye."]
# --- your code below -------------------------------------------------------
path = "greeting.txt"
# TODO: open(path, "w") and write each line in `lines` (remember "\n")
contents = ... # TODO: open(path) and read the whole file
# --- self-check (don't edit) ----------------------------------------------
assert contents == "Hello, world!\nWelcome to Python.\nGoodbye.\n", \
f"contents was {contents!r}"
print("✅ Passed!", "file contents:", repr(contents))
os.remove(path)