Python Read Files
To read a file, use the open()
function with the 'r'
mode. After reading, always remember to close the file using close()
or use a with
statement to automatically handle closing.
Example: Reading a File
# Reading a file
with open('example.txt', 'r') as file:
content = file.read()
print(content)
Output
[Contents of example.txt]
Explanation: In this example, the file example.txt
is opened in read mode, and its content is printed to the console.