import time import sys from subprocess import run # Read all lines from the file initially with open('inp.txt', 'r') as f: lines = f.readlines() try: for index, line in enumerate(lines): stripped = line.strip() # Skip empty lines and lines that are already commented out if not stripped or stripped.startswith('#'): continue # Announce the URL print(f"Downloading: {stripped}") try: # Execute the download result = run(['curl', '-H', 'referer: https://klim.co.nz/', '-O', stripped]) # If successful, comment out the line in the file immediately if result.returncode == 0: lines[index] = f"# {line}" # Update the file on disk instantly with open('inp.txt', 'w') as f: f.writelines(lines) time.sleep(0.5) except KeyboardInterrupt: # Catch Ctrl+C during curl and re-raise to exit outer loop raise except KeyboardInterrupt: print("\nScript interrupted by user. Exiting safely...") sys.exit(1)