Python File Handling
Python Read Files
Reading from files
Python Read Files
To read a file in Python, you can use the open() function with the file path and mode.
Reading a File
f = open("demofile.txt", "r")
print(f.read())
f.close()
Using with Statement
The with statement automatically closes the file:
with open("demofile.txt", "r") as f:
content = f.read()
print(content)
Read Line by Line
with open("demofile.txt", "r") as f:
for line in f:
print(line)