How to automatically detect and copy disc content in the background

Source: Internet
Author: User

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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.