Last active
August 2, 2022 17:46
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
great!