import os import readline import argparse from datetime import datetime import time # Tab completion setup for file paths def complete_path(text, state): expanded = os.path.expanduser(os.path.expandvars(text)) if os.path.isdir(expanded): try: entries = os.listdir(expanded) matches = [os.path.abspath(os.path.join(expanded, entry)) + '/' if os.path.isdir(os.path.join(expanded, entry)) else os.path.abspath(os.path.join(expanded, entry)) for entry in entries] except FileNotFoundError: matches = [] else: dirname = os.path.dirname(expanded) or '.' basename = os.path.basename(expanded) try: entries = [entry for entry in os.listdir(dirname) if entry.startswith(basename)] matches = [os.path.abspath(os.path.join(dirname, entry)) for entry in entries] except FileNotFoundError: matches = [] matches.sort() try: return matches[state] except IndexError: return None readline.set_completer_delims(' \t\n;') readline.set_completer(complete_path) readline.parse_and_bind('tab: complete') # Parse command-line arguments parser = argparse.ArgumentParser(description="Write messages to a file.") parser.add_argument("filename", nargs="?", help="Path to the file to write to.") args = parser.parse_args() # Ask for filename if not provided file_path = args.filename or input("Enter filename: ") # Message writing loop name = "hi" age = 30 max_iterations = 1000 try: for i in range(max_iterations): timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") message = "[{}] My name is {} and I am {} years old.\n".format(timestamp, name, age) with open(file_path, "a") as file: file.write(message) time.sleep(1) except KeyboardInterrupt: print("\n[Interrupted] Ctrl+C detected. Stopped writing to file.")