我已经处理了一段时间了。我不认为我能找到一个简单的解决方案。我想知道的是我玩游戏的时间。
为什么打印result_1
有时会返回0而另外1次?为什么它没有返回"没有任务正在运行......"或者告诉我正在运行的任务?
import subprocess
from time import *
from datetime import *
command_1 = "TASKLIST /FI \"IMAGENAME eq Application-steam*\"" #Code for finding the "Banished" game
no_task_respond_0 = "INFO: No tasks are running which match the specified criteria."
while True:
the_time = datetime.now()
result_1 = subprocess.call(command_1, shell=True) # running this code prints the result whether i want it or not
open_file = open("watcher.txt","w")
current_task_file = open_file.write(str(result_1))
print(current_task_file,"Inside of Current task FILE!!")
if str(current_task_file) == str(no_task_respond_0): #If no games are running then continue
continue
else: #If the game started then note the time
date_file = open("datefile.txt","a")
date_watcher = date_file.write(str(the_time)+"\n")
sleep(4)
当我关闭游戏时,这是它返回的结果:
**INFO: No tasks are running which match the specified criteria.**
**1 Inside of Current Task FILE!!**
这是游戏运行时的输出:
Image Name PID Session Name Session# Mem Usage
========================= ======== ================ =========== ============
Application-steam-x64.exe 14684 Console 1 298.588 K
**1 Inside of Current task FILE!!**
这些1来自打印result_1
。