with open("example.txt", "r") as file:
for line in file:
print(line.strip())
#source code --> clcoding.com
Code Explanation:
Open the File in Read Mode ("r")
open("example.txt", "r") opens the file in read mode.
If the file does not exist, Python will raise a FileNotFoundError.
Read the File Line by Line
for line in file: iterates through each line in the file.
Print Each Line Separately
print(line.strip()) prints the line after using .strip(), which removes extra spaces or newline characters (\n) from the output.
0 Comments:
Post a Comment