Skip to content

Instantly share code, notes, and snippets.

@AlexanderHott
Created March 28, 2024 15:33
Show Gist options
  • Save AlexanderHott/8e6c6d07edbd54a73ea7573e0f8b2290 to your computer and use it in GitHub Desktop.
Save AlexanderHott/8e6c6d07edbd54a73ea7573e0f8b2290 to your computer and use it in GitHub Desktop.
python file that moves randomly somewhere on your Desktop
import os
import random
import logging
import sys
import subprocess
# Locate movable scope
if os.name == "nt":
homedir = os.getenv("HOMEDRIVE") + os.getenv("HOMEPATH") + os.sep + "Desktop"
elif os.name == "posix":
homedir = os.getenv("HOME") + os.sep + "Desktop"
# Get a random folder
paths = [*os.walk(homedir)]
path = random.choice(paths)
# Log folder path to log file on desktop
LOG_FORMAT = "%(asctime)s:%(levelname)s:%(filename)s:%(message)s"
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
formatter = logging.Formatter(LOG_FORMAT)
file_handler = logging.FileHandler(os.path.join(homedir, "adv.log"))
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
logger.info(f"Moved to {path[0]}")
# read self
with open(__file__, "r") as f:
adv = f.read()
# write to new file
with open(os.path.join(path[0], "adventure.py"), "w") as f:
f.write(adv)
# delete self
if os.name == "nt":
subprocess.run(
["python3", "-c", f"import os, time; time.sleep(1); os.remove('{sys.argv[0]}');"],
shell=True,
)
else:
subprocess.run(
["python3", "-c", f"import os, time; time.sleep(1); os.remove('{sys.argv[0]}');"])
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment