63 lines
2.2 KiB
Python
63 lines
2.2 KiB
Python
import os
|
|
import readline
|
|
import argparse
|
|
from datetime import datetime
|
|
import time
|
|
|
|
# --- Tab Completion ---
|
|
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')
|
|
|
|
# --- Argument Parsing ---
|
|
parser = argparse.ArgumentParser(description="Write messages to a file with optional verbosity and interval.")
|
|
parser.add_argument("filename", nargs="?", help="Path to the file to write to.")
|
|
parser.add_argument("--verbose", action="store_true", help="Print each message to console.")
|
|
parser.add_argument("--interval", type=float, default=1.0, help="Delay between writes in seconds.")
|
|
args = parser.parse_args()
|
|
|
|
# --- Filename prompt fallback ---
|
|
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 = f"[{timestamp}] My name is {name} and I am {age} years old.\n"
|
|
with open(file_path, "a") as file:
|
|
file.write(message)
|
|
if args.verbose:
|
|
print(message.strip())
|
|
time.sleep(args.interval)
|
|
except KeyboardInterrupt:
|
|
print("\n[Interrupted] Ctrl+C detected. Stopped writing to file.")
|