Knowledge organisersFile Handling Techniques
Read lines or whole file.
You can read a whole file at once with .read(), get a list of lines with .readlines(), or loop through line by line with a for loop. Each line includes a newline character.
.read() returns the entire file as one string.readlines() returns a list of linesfor line in f: loops one line at a time.strip() to remove trailing newline from each linewith open("names.txt", "r") as f:
for line in f:
name = line.strip()
print("Hello, " + name)