问题
I wrote a python script which often sends mails via Outlook. The problem is if I don't have Outlook open, no mails will be send. (I use Outlook 2013) Now I want to check at the beginning of my script if Outlook is already open, and if not I want to open it with python to avoid the errors.
How do I check if Outlook is already running via Python 2.7 ? To start Outlook I would use the simple code:
import os
os.startfile("outlook")
Thanks for your help!
回答1:
Since the OP asks about checking for Outlook, i'm guessing this is for windows and not unix. The issue has been covered before in another question where you can find further details.
I have tried the following snippet which works fine on python 3.4. I only had to install the win32 package via pip install pypiwin32
.
def outlook_is_running():
import win32ui
try:
win32ui.FindWindow(None, "Microsoft Outlook")
return True
except win32ui.error:
return False
if not outlook_is_running():
import os
os.startfile("outlook")
回答2:
Check to see what process is started by outlook, and then use this code to check if that process is running:
def isprocessrunning(process):
import re
import subprocess
running = False
processlist = subprocess.Popen(["ps", "ax"],stdout=subprocess.PIPE)
for a in processlist.stdout:
if re.search(process, a):
running = True
return running
#For Example,
isprocessrunning("chrome".encode('utf-8'))
#Returns True if Chrome is running
Reference: Check if a process is running in Python (in Linux/Unix)
来源:https://stackoverflow.com/questions/33867432/check-with-python-if-outlook-is-already-open-if-not-open-it