Principle: Using Python's Win32 module, registering services, letting code run in the background, detecting discs and copying files
The way to start is directly under CMD, main.py Install, and then go to Windows Services can see the smallestpossible Python service, you can start, stop, can also be set up to boot automatically. After the service is started, the disc is automatically detected and the files are copied in the background
main.py
Import win32serviceutil Import win32service import win32event import Copydvd2hard class Smallestpyt Honservice (Win32serviceutil. Serviceframework): _svc_name_ = "Smallestpythonservice" _svc_display_name_ = "smallest possible Python servi CE "def __init__ (self, args): Win32serviceutil.
Serviceframework.__init__ (self, args) # Create an event which we'll use to ' wait ' on.
# the "Service Stop" request would set this event. Self.hwaitstop = win32event. CreateEvent (None, 0, 0, None) def svcstop (self): # before we do anything, tell the SCM we are start
ing the stop process. Self. Reportservicestatus (Win32service.
service_stop_pending) # and set my event. Win32event.
SetEvent (self.hwaitstop) def svcdorun (self): #实际运行代码 # Copydvd2hard.copydvd2hard () Win32event. WaitForSingleObject (Self.hwaitstop, win32event. INFINITE) If __name__== ' __main__ ': Win32serviceutil. Handlecommandline (Smallestpythonservice)
copydvd2hard.py
__author__ = ' M '
import os
import win32file
import shutil
import time
def copydvd2hard (dstdir= ' Temp '):
#检测本地的最后一个光驱和硬盘分区
letters = [L.upper () + ': ' for L in ' ABCDEFGHIJKLMNOPQRSTUVWXYZ ']
cdrom = '
harddrive = ' for
drive in letters:
if win32file. GetDriveType (drive) = = 3:
harddrive = drive
if Win32file. GetDriveType (drive) = = 5:
cdrom = drive cdrom + = '
/'
harddrive = harddrive + '/' + Dstdir
#检测光驱内是否有光盘, Wait for disc to insert while
os.system (' cd/d ' + cdrom):
time.sleep (5)
#拷贝文件
shutil.copytree (CDROM, harddrive)
if __name__ = = ' __main__ ':
copydvd2hard ()
Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/