I'm learning the Python programming language with the book "Self-taught Programmer" and just really starting out. no idea or knowledge previously so I'm currently stuck on working with files. I'm learning with Python311.
Trying to replicate the below codes from the book "Self-taught programmer"
with open('my_file.txt','w') as my_file:
my_file.write('Hello from Python!')
with open("my_file.txt", "r") as my_file:
for line in my_file.read():
print(line)
Output in book:
Hello from Python!
But mine (with same syntax) prints
H
e
l
l
o
f
r
o
m
P
y
t
h
o
n
!
my_file.read()returns a string, not a list of lines. Either usefor line in my_file:orfor line in my_file.readlines()