检查是否有同名py文件在运行

import psutil
import os

def is_running(script):
for q in psutil.process_iter():
if q.name().startswith('python'):
if len(q.cmdline())>1 and script in q.cmdline()[1] and q.pid !=os.getpid():
print("'{}' Process is already running".format(script))
return True

return False


if not is_running("111.py"):
n = input("What is Your Name? ")
print ("Hello " + n)
原文地址:https://www.cnblogs.com/wangdongpython/p/10998616.html