简体   繁体   English

在退出cmd提示符后,如何编写重新执行python脚本的脚本?

[英]How do I write a script that re-execute a python script after it exits back to cmd prompt?

I have a pythonscript run.py that I currently run in the command line. 我有一个pythonscript run.py,我目前在命令行中运行。 However, I want a start.py script either in python (preferably) or .bat, php, or some other means that allows me to make it such that once run.py finishes running, the start.py script will reexecute the run.py script indefinitely, but ONLY after the run.py finishes executing and exits. 但是,我想要一个start.py脚本(最好是python)或.bat,php或其他一些允许我运行的方法,以便在run.py完成运行后,start.py脚本将重新执行运行。 py脚本是无限期的,但只有在run.py完成执行并退出后才可以。

Sample Steps: - Start.py is run that starts Run.py - Run.py prints "hello" for 3 times after 5 seconds and exits normally or abnormally - Start.py knows Run.py finished/closed and reexecutes run.py 示例步骤:-运行Start.py并启动Run.py-Run.py在5秒后打印“ hello” 3次,并正常或异常退出-Start.py知道Run.py已完成/关闭并重新执行run.py

How do I accomplish this? 我该如何完成?

Is the first run interactive? 首次运行是否具有交互性? Otherwise, you may use a daemon 否则,您可以使用守护程序

If it's just a Python script, you could do this: 如果只是Python脚本,则可以执行以下操作:

import os

while True:
  os.system('python run.py')

But a better approach would be to make your other Python file modular and run it properly via the second Python file. 但是更好的方法是使您的其他Python文件模块化,并通过第二个Python文件正确运行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM