2

I'm very new to python and I am trying to write a short script that simply opens file explorer then closes it again, I can get it to open fine using the following..

import os
import subprocess
os.startfile("Y:\\")

I tried adding the following to then close the explorer window but this seems to do nothing

os.system("taskkill /f /im explorer.exe")

I'm sure I'm missing something simple...

Thanks

Scott P
  • 21
  • 1
  • 3
  • If you are using os.system then you need to look at the windows documentation/forumns. os.system just executes commands exactly as if you had typed them into command line. I'm not sure what the answer is here, but for this sort of gui automation task, it's often easier to use pygui. Then you just program your mouse to move to the exit button, and click it! – Neil Sep 10 '18 at 08:44

1 Answers1

0
import psutil
from subprocess import PIPE

#How to open a process
psutil.Popen(['SnippingTool.exe'],stdout=PIPE)

#How to kill a process
TARGET = "chrome.exe"
[process.kill() for process in psutil.process_iter() if process.name() == TARGET]
Jonas
  • 1,838
  • 4
  • 19
  • 35
Dan Mahowny
  • 163
  • 1
  • 10
  • the first line should be broken down in 2 as such: – Dan Mahowny Sep 10 '18 at 08:59
  • from subprocess import PIPE – Dan Mahowny Sep 10 '18 at 09:00
  • While this might answer the authors question, it lacks some explaining words and links to documentation. Raw code snippets are not very helpful without some phrases around it. You may also find [how to write a good answer](https://stackoverflow.com/help/how-to-answer) very helpful. Please edit your answer. – hellow Sep 10 '18 at 09:10
  • this gives the error.. ModuleNotFoundError: No module named 'psutil' – Scott P Sep 10 '18 at 10:34
  • You will need to install it, see [psutil](https://pypi.org/project/psutil/). Use `pip install psutil` – Martin Evans Sep 10 '18 at 11:16
  • Thank you! That's what I couldn't figure out.. I needed to install psutil but I didn't realise I needed to type that into a cmd window – Scott P Sep 10 '18 at 16:13