Skip to content

Instantly share code, notes, and snippets.

@TechStudent10
Last active August 2, 2022 17:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TechStudent10/610a8665ba0aa175ad2547402ac5f028 to your computer and use it in GitHub Desktop.
Save TechStudent10/610a8665ba0aa175ad2547402ac5f028 to your computer and use it in GitHub Desktop.
A Python script that restarts another Python script when it detects changes in it.
import os, sys, platform
currentFile = ""
while currentFile == "" or currentFile == None:
currentFile = input("What is the file: ")
currentInfo = ""
def check():
global currentFile
global currentInfo
file = open(currentFile, "r")
data = file.read()
if data != currentInfo:
currentInfo = data
return True
else:
return False
file.close()
running = True
while running:
try:
fileHasChanged = check()
if fileHasChanged:
print(currentFile, "has changed")
if platform.system() == "Windows":
os.system("python " + currentFile)
else:
os.system("python3 " + currentFile)
except KeyboardInterrupt:
sys.exit()
@yaya2devops
Copy link

great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment