http://timgolden.me.uk/pywin32-docs/contents.html
https://pypi.org/project/pywin32/#files
下载python版本对应的pywin32版本
pywin32-227-cp38-cp38-win32.whl(要用32位的)
# 安装227版本的pywin32
# 方法一:pip install pywin32==227 # 方法二:pip install "C:\Users\DZL\Downloads\pywin32-227-cp38-cp38-win32.whl"
# 删除文件时不会提示文件被占用
import os import win32file import msvcrt filename = "开票软件日志.log" # get an handle using win32 API, specifyng the SHARED access! handle = win32file.CreateFile(filename, win32file.GENERIC_READ, win32file.FILE_SHARE_DELETE | win32file.FILE_SHARE_READ | win32file.FILE_SHARE_WRITE, None, win32file.OPEN_EXISTING, 0, None) # detach the handle detached_handle = handle.Detach() # get a file descriptor associated to the handle file_descriptor = msvcrt.open_osfhandle( detached_handle, os.O_RDONLY) # open the file descriptor file = open(file_descriptor) for line in file: print(line)
创建 Windows服务
''' SMWinservice Base class to create winservice in Python ----------------------------------------- Instructions: 1. Just create a new class that inherits from this base class 2. Define into the new class the variables _svc_name_ = "nameOfWinservice" _svc_display_name_ = "name of the Winservice that will be displayed in scm" _svc_description_ = "description of the Winservice that will be displayed in scm" 3. Override the three main methods: def start(self) : if you need to do something at the service initialization. A good idea is to put here the inizialization of the running condition def stop(self) : if you need to do something just before the service is stopped. A good idea is to put here the invalidation of the running condition def main(self) : your actual run loop. Just create a loop based on your running condition 4. Define the entry point of your module calling the method "parse_command_line" of the new class 5. Enjoy ''' # SMWinservice.py import socket import win32serviceutil import servicemanager import win32event import win32service class SMWinservice(win32serviceutil.ServiceFramework): '''Base class to create winservice in Python''' _svc_name_ = 'pythonService' _svc_display_name_ = 'Python Service' _svc_description_ = 'Python Service Description' @classmethod def parse_command_line(cls): ''' ClassMethod to parse the command line ''' win32serviceutil.HandleCommandLine(cls) def __init__(self, args): ''' Constructor of the winservice ''' win32serviceutil.ServiceFramework.__init__(self, args) self.hWaitStop = win32event.CreateEvent(None, 0, 0, None) socket.setdefaulttimeout(60) def SvcStop(self): ''' Called when the service is asked to stop ''' self.stop() self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) win32event.SetEvent(self.hWaitStop) def SvcDoRun(self): ''' Called when the service is asked to start ''' self.start() servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STARTED, (self._svc_name_, '')) self.main() def start(self): ''' Override to add logic before the start eg. running condition ''' pass def stop(self): ''' Override to add logic before the stop eg. invalidating running condition ''' pass def main(self): ''' Main class to be ovverridden to add logic ''' pass # entry point of the module: copy and paste into the new module # ensuring you are calling the "parse_command_line" of the new created class if __name__ == '__main__': SMWinservice.parse_command_line() # PythonCornerExample.py import time import random from pathlib import Path from SMWinservice import SMWinservice class PythonCornerExample(SMWinservice): _svc_name_ = "PythonCornerExample" _svc_display_name_ = "Python Corner's Winservice Example" _svc_description_ = "That's a great winservice! :)" def start(self): self.isrunning = True def stop(self): self.isrunning = False def main(self): i = 0 while self.isrunning: random.seed() x = random.randint(1, 1000000) Path(f'c:\\{x}.txt').touch() time.sleep(5) if __name__ == '__main__': PythonCornerExample.parse_command_line()
安装winservice服务,在脚本所在路径打开cmd
#1.安装服务
python PythonService.py install
#2.让服务自动启动
python PythonService.py --startup auto install
#3.启动服务
python PythonService.py start
#4.重启服务
python PythonService.py restart
#5.停止服务
python PythonService.py stop
#6.删除/卸载服务
python PythonService.py remove
————————————————
C:\python2winservice>python PythonCornerExample.py insall
将来,如果您想更改服务代码,只需对其进行修改并使用以下命令重新安装该服务
C:\python2winservice>python PythonCornerExample.py update
现在,打开“服务” msc管理单元
C:\python2winservice>mmc Services.msc
找到PythonCornerExample,右键启动服务
如果报错
确保文件"C:\Python38\Lib\site-packages\win32\pywintypes38.dll"存在(请注意,“38”是您的Python安装版本)
如果没有此文件,请从"C:\Python38\Lib\site-packages\pywin32_system32\pywintypes38.dll"中获取
C:\python2winservice>python PythonCornerExample.py debug
将以下添加到环境变量path中
C:\Python38;
C:\Python38\Scripts;
C:\Python38\Lib\site-packages\win32;
C:\Python38\Lib\site-packages\pywin32_system32;
评论
发表评论