app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
def write_to_file(filename, content): with open(filename, 'w') as file: file.write(content) print(f"[✓] Written to {filename}") def append_to_file(filename, content): with open(filename, 'a') as file: file.write(content) print(f"[✓] Appended to {filename}") def read_file(filename): try: with open(filename, 'r') as file: data = file.read() print(f"\n[📄] Contents of {filename}:\n") print(data) except FileNotFoundError: print(f"[✗] File '{filename}' not found.") def main(): filename = "example.txt" while True: print("\n==== File Operations Menu ====") print("1. Write to file (overwrites)") print("2. Append to file") print("3. Read file") print("4. Exit") choice = input("Enter your choice (1-4): ") if choice == '1': content = input("Enter text to write: ") + "\n" write_to_file(filename, content) elif choice == '2': content = input("Enter text to append: ") + "\n" append_to_file(filename, content) elif choice == '3': read_file(filename) elif choice == '4': print("Exiting program.") break else: print("Invalid choice. Please try again.") if __name__ == "__main__": main() |