C:\Users\andgein on Windows, /home/andgein on Linux, and /Users/andgein on macOSopen("C:\new\test.txt") look correct?open, import os, and related APIsimport pathlibfrom pathlib import Path
from tempfile import gettempdir
# E.g. /tmp on Linux, /var/folders/.../T on macOS,
# C:\Users\<user>\AppData\Local\Temp on Windows
path = Path(gettempdir()) / "notes.txt"
file = path.open("w")
file.write("Operator := is called a walrus operator\n")
file.write("Operator |= merges another dictionary into this dictionary in place\n")
file.close()
You must always close open files. But what happens if an error is raised between open() and close()?
from pathlib import Path
from tempfile import gettempdir
# E.g. /tmp on Linux, /var/folders/.../T on macOS,
# C:\Users\<user>\AppData\Local\Temp on Windows
path = Path(gettempdir()) / "notes.txt"
with path.open("w") as file:
file.write("Operator := is called a walrus operator\n")
file.write("Operator |= merges another dictionary into this dictionary in place\n")
Another approach
path.write_text(
"Operator := is called a walrus operator\n"
"Operator |= merges another dictionary into this dictionary in place\n"
)
It does not require opening or closing the file, and it replaces its entire contents
with path.open("r") as file:
line = file.readline() # Read the next line
lines = file.readlines() # Read remaining lines into a list
with path.open("r") as file:
part = file.read(100) # Read the next 100 characters
content = file.read() # Read all remaining text
with path.open("r") as file:
for line in file: # Iterate over lines
print(line)
content = path.read_text() # Read all text at once
By default, reading starts at the beginning of a file and advances its position with each read
"rb" or "wb" for binary files, or the read_bytes() and write_bytes() shortcutswith path.open("rb") as file:
content = file.read()
print(content)
bytes and behaves much like a list of integers from 0 to 255print(list(b"Operator := is called a walrus operator\n"))
| Code | Character | Code | Character | Code | Character | Code | Character |
|---|---|---|---|---|---|---|---|
| 00 | Null | 32 | Space | 64 | @ | 96 | ` |
| 01 | Start heading | 33 | ! | 65 | A | 97 | a |
| 02 | Start text | 34 | " | 66 | B | 98 | b |
| 03 | End text | 35 | # | 67 | C | 99 | c |
| 04 | End transmission | 36 | $ | 68 | D | 100 | d |
| 05 | Enquiry | 37 | % | 69 | E | 101 | e |
| 06 | Acknowledge | 38 | & | 70 | F | 102 | f |
| 07 | Bell | 39 | ' | 71 | G | 103 | g |
| 08 | Backspace | 40 | ( | 72 | H | 104 | h |
| 09 | Tab | 41 | ) | 73 | I | 105 | i |
| 10 | Line feed | 42 | * | 74 | J | 106 | j |
| 11 | Vertical tab | 43 | + | 75 | K | 107 | k |
| 12 | Form feed | 44 | , | 76 | L | 108 | l |
| 13 | Carriage return | 45 | - | 77 | M | 109 | m |
| 14 | Shift out | 46 | . | 78 | N | 110 | n |
| 15 | Shift in | 47 | / | 79 | O | 111 | o |
| 16 | Data escape | 48 | 0 | 80 | P | 112 | p |
| 17 | Control 1 | 49 | 1 | 81 | Q | 113 | q |
| 18 | Control 2 | 50 | 2 | 82 | R | 114 | r |
| 19 | Control 3 | 51 | 3 | 83 | S | 115 | s |
| 20 | Control 4 | 52 | 4 | 84 | T | 116 | t |
| 21 | Negative ack | 53 | 5 | 85 | U | 117 | u |
| 22 | Synchronous idle | 54 | 6 | 86 | V | 118 | v |
| 23 | End block | 55 | 7 | 87 | W | 119 | w |
| 24 | Cancel | 56 | 8 | 88 | X | 120 | x |
| 25 | End medium | 57 | 9 | 89 | Y | 121 | y |
| 26 | Substitute | 58 | : | 90 | Z | 122 | z |
| 27 | Escape | 59 | ; | 91 | [ | 123 | { |
| 28 | File separator | 60 | < | 92 | \ | 124 | | |
| 29 | Group separator | 61 | = | 93 | ] | 125 | } |
| 30 | Record separator | 62 | > | 94 | ^ | 126 | ~ |
| 31 | Unit separator | 63 | ? | 95 | _ | 127 | Delete |
text = "⌘"
print(text.encode())
print(list(text.encode()))
requests, httpx2, or aiohttpimport requests
response = requests.get("https://swapi.dev/api/planets/")
data = response.json()
for planet in data["results"]:
print(planet["name"], planet["population"], planet["url"], sep=" — ")
Let’s iterate over pages and make some other improvements
import requests
url = "https://swapi.dev/api/planets/"
while url:
response = requests.get(url, timeout=10)
response.raise_for_status()
data = response.json()
for planet in data["results"]:
print(planet["name"], planet["population"], planet["url"], sep=" — ")
url = data["next"]
Flask, FastAPI, Django, uvicorn, and gunicorn