首页›文章›python遍历目录并打印文件

python遍历目录并打印文件

计算机技术 · python 程序开发2013-09-221次浏览0 个评论
python
# -*- coding: gbk -*-
import os
import shutil
import win32print
import win32api
fileList = []
sorDir = os.getcwd()
dstDir = sorDir + '.bak'
compressFileFormat = ['rar','zip','7z','ace','arj','bz2','cab','gz','iso','jar','lzh','tar','uue','z']
WinRARPath = 'D:\\Programs\\winRAR\\WinRAR.exe'
newFileList = []
myPrinter = ''
def getFileList(fileList,sorDir):
    for root,dirs,files in os.walk(sorDir,topdown=True):
        for file in files:
            fileList.append(os.path.join(root,file))
def mkdir(dstDir):
    try:
        os.mkdir(dstDir)
    except OSError:
        pass
def copyFile(file,dstDir):
    try:
        shutil.copy(file,dstDir)
    except OSError:
        pass
def isCompressFile(file):
    if (os.path.splitext(file)[1][1:].lower() in compressFileFormat)== True:
        return True
    else:
        return False
def deCompressFile(file,dstDir):
    cmd =WinRARPath + ' x -ibck ' + file + ' * ' + dstDir + '\\'
    os.system(cmd)
def getWinRARPath():
    global WinRARPath
    print "请输入WinRAR的安装路径,例如\nD:\\Programs\\winRAR\\WinRAR.exe"
    WinRARPath = raw_input()
def initPrinter():
    global myPrinter
    myPrinter = win32print.GetDefaultPrinter()
    print "当前默认打印机:" + myPrinter
    print "是否设置其他打印机为默认打印机?(Y/N)"
    res = raw_input()
    if res.upper() == 'Y':
        myPrinter = res
        win32print.SetDefaultPrinter(myPrinter)
def printFile(file,myPrinter):
    PH = win32print.OpenPrinter(myPrinter)
    print '开始打印文档:' + file
    res = win32print.StartDocPrinter(PH,1,(file,None,'RAW'))
    print '打印文档' + file + '完成'
    PH.close()
if __name__ == '__main__':
    mkdir(dstDir)
    getFileList(fileList,sorDir)
    getWinRARPath()
    for file in fileList:
        if isCompressFile(file):
            deCompressFile(file,dstDir)
        else:
            copyFile(file,dstDir)
    getFileList(newFileList,dstDir)
    initPrinter()
    for newfile in newFileList:
        printFile(newfile,myPrinter)
#        win32api.ShellExecute(0,'print',newfile,'','',0)

评论

0

评论加载中…

发表评论

0/2000