用Python关闭Windows可执行程序系列方法

方法一:os模块中的 system()函数

# subprocess 模块中的 Popen()函数
# 1、导入subprocess包
import subprocess
# 打开记事本
app=subprocess.Popen('notepad')
# # 打开微信
# app1=subprocess.Popen('"C:\Program Files (x86)\Tencent\WeChat\WeChat.exe"')
# 打开迅雷
app2=subprocess.Popen('"C:\Program Files (x86)\Thunder Network\Thunder\Program\ThunderStart.exe"')

# 方法一
# 用OS模块OS.system(‘taskkill /f /程序名’)
# 导入模块
import os
import time

time.sleep(10)
# 关闭记事本
gbapp1=os.system('C:\\Windows\\System32\\taskkill /F /IM notepad.exe')
# 关闭迅雷
gbapp2=os.system('C:\\Windows\\System32\\taskkill /F /IM Thunder.exe')

方法二:subprocess 模块中的 Popen()函数

# coding=gbk
# subprocess 模块中的 Popen()函数
# 1、导入subprocess包
from ctypes import *
import subprocess
# 打开记事本
app=subprocess.Popen('notepad')
# # 打开微信
# app1=subprocess.Popen('"C:\Program Files (x86)\Tencent\WeChat\WeChat.exe"')
# 打开迅雷
app2=subprocess.Popen('"C:\Program Files (x86)\Thunder Network\Thunder\Program\ThunderStart.exe"')



# 方法二
# 调用subprocess.Popen('C:\\Windows\\System32\\taskkill /F /IM 程序名')文件关闭程序
import time
time.sleep(4)
app1_gb=subprocess.Popen('C:\\Windows\\System32\\taskkill /F /IM notepad.exe')

app2_gb=subprocess.Popen('C:\\Windows\\System32\\taskkill /F /IM Thunder.exe')

方法三:os 模块中的 system()函数

# coding=gbk
# subprocess 模块中的 Popen()函数
# 1、导入subprocess包
import subprocess
# 打开记事本
app=subprocess.Popen('notepad')
# # 打开微信
# app1=subprocess.Popen('"C:\Program Files (x86)\Tencent\WeChat\WeChat.exe"')
# 打开迅雷
app2=subprocess.Popen('"C:\Program Files (x86)\Thunder Network\Thunder\Program\ThunderStart.exe"')

# 方法三
# 关闭电源(立即和定时)
# OS.system(shutdown -s -t 时间)
# OS.system(shutdown -s -t 时间 -m ip地址)关闭远程电脑电源
# 导入模块
import os
# os.system('shutdown -s -t 100')
os.system('shutdown -s -t 100 -m 127.0.0.1')

方法四:用pyautogui.hotkey("alt","f4")

# coding=gbk
# subprocess 模块中的 Popen()函数
# 1、导入subprocess包
from ctypes import *
import subprocess
# 打开记事本
app=subprocess.Popen('notepad')
# # 打开微信
# app1=subprocess.Popen('"C:\Program Files (x86)\Tencent\WeChat\WeChat.exe"')
# 打开迅雷
app2=subprocess.Popen('"C:\Program Files (x86)\Thunder Network\Thunder\Program\ThunderStart.exe"')
import time


# 方法四
# 一、导入模块
import pyautogui


# 等待5秒,关闭软件
time.sleep(5)
pyautogui.hotkey("alt","f4")

time.sleep(5)
pyautogui.hotkey("alt","f4")

方法五:win32gui.PostMessage函数

import time
from pywinauto.application import Application
app = Application(backend="win32")
# 方法五
# 一、导入模块
import pyautogui
import win32gui
import win32con

astring = u'天下第八道!'
astrToint = [ord(c) for c in astring]
# 打开记事本
app.start(r"C:\Windows\System32\notepad.exe")

time.sleep(2)
pyautogui.hotkey('alt','f')
time.sleep(1)
pyautogui.hotkey('ctrl','n')
time.sleep(1)
pyautogui.hotkey('ctrl','shift','s')
time.sleep(2)
wjm='diqidao'
pyautogui.typewrite(wjm)
time.sleep(2)
pyautogui.hotkey('enter')
time.sleep(2)
pyautogui.hotkey('enter')

time.sleep(5)
hd=win32gui.FindWindow('Notepad',f'{wjm}.txt - 记事本')
hWndEdit = win32gui.FindWindowEx(hd,None,"Edit",None)

for x in astrToint:
	win32gui.SendMessage(hWndEdit,win32con.WM_CHAR,x,0)

time.sleep(2)
pyautogui.hotkey('enter')
time.sleep(1)
pyautogui.hotkey('ctrl','s')

print('hd:',hd)
win32gui.SetForegroundWindow(hd)
win32gui.PostMessage(hd,win32con.WM_CLOSE,0,0)

申明:以上方法参照了互联网其他博主,自己试验后整理,请勿用于违法犯罪所用

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiao001yue

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值