You can use the built-in function open()
in Python to open a file in read mode and then use a for
loop to iterate over the lines in the file one by one. Here is an example:
# Open the file in read mode
file = open('filename.txt', 'r')
# Iterate over the lines in the file and print each line
for line in file:
print(line)
# Close the file when finished
file.close()
In this example, replace ‘filename.txt’ with the name of the file you want to read. The for
loop will iterate over the lines in the file one by one, and the print()
function will print each line to the console as it is read. Finally, the close()
method is used to close the file after reading.
Asked: 2023-06-07 11:59:42 +0000
Seen: 7 times
Last updated: Jun 07 '23