Python file usage
- Python Code for File Usage
# 1. Create & Write to a file
with open("example.txt", "w") as file:
file.write("Hello, this is line one.\n")
file.write("This is line two.\n")
print("File created and written successfully.")
# 2. Read the file
with open("example.txt", "r") as file:
content = file.read()
print("\nReading file content:")
print(content)
# 3. Append to the file
with open("example.txt", "a") as file:
file.write("This line was added later.\n")
print("Line appended successfully.")
# 4. Read again to verify
with open("example.txt", "r") as file:
print("\nUpdated file content:")
print(file.read())
thanks for the code
ReplyDelete