Python automatically restarts the program.

Source: Internet
Author: User

Python automatically restarts the program.

This example describes how to automatically restart the program in python. Share it with you for your reference. The specific implementation method is as follows:

#! /Usr/local/bin/python #-*-coding: UTF-8 -*-#################################### ################################# python automatically restarts the program ## ######################################## ########################## import OS, time # def close (): # print "restart the program !!!! "# Print time. strftime ('% Y. % m. % d-% H. % M. % s') # time. sleep (2) #3 seconds # p = OS. popen ('2017. bat ') # while True: # line = p. readline (); # if ''= line: # break # print line # if _ name _ = '_ main _': # close () ######################################## ########################### import timeimport sysimport osdef restart_program (): python = sys.exe cutable OS .exe cl (python, python, * sys. argv) if _ name _ = "_ main _ ": Print 'start... '# answer = raw_input (" Do you want to restart this program? ") # If answer. strip () in "y Y y yes Yes YES ". split (): # restart_program () print u "3 seconds later, the program will end... ". encode ("gbk") time. sleep (3) restart_program ()

Shows the running effect:

I hope this article will help you with Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.