使用Python检查Windows上是否存在PID,而不需要库

有没有一种方法来检查是否在Windows上使用Python存在PID而不需要库? 如何?

这是用一小杯WINAPI解决的。

 def pid_running(pid): import ctypes kernel32 = ctypes.windll.kernel32 SYNCHRONIZE = 0x100000 process = kernel32.OpenProcess(SYNCHRONIZE, 0, pid) if process != 0: kernel32.CloseHandle(process) return True else: return False 

这在我的系统上工作..

 >>> import subprocess >>> out = subprocess.check_output(["tasklist","/fi","PID eq 1234"]).strip() >>> if out == "INFO: No tasks are running which match the specified criteria.": ... print "No such PID :D" ... No such PID :D