Working with Files
File Handling Concepts Table
SI
Section
Usage Scenario
Pros
Cons
Code Sample
with open('example.txt', 'r') as file:
data = file.read()
print(data)# Reading a file
with open('example.txt', 'r') as file:
content = file.read()
# Writing to a file
with open('output.txt', 'w') as file:
file.write("Hello, World!")with open('large_file.txt', 'r') as file:
for line in file:
print(line.strip())# Read the entire file
with open('example.txt', 'r') as file:
content = file.read()
# Read all lines into a list
with open('example.txt', 'r') as file:
lines = file.readlines()
# Read one line
with open('example.txt', 'r') as file:
line = file.readline()Last updated