python 剪贴板与文件复制操作

#coding:utf-8
import shutil
import time
import traceback
import json
import urllib
import re
import os
import sys
import logging
import datetime


import win32clipboard as w  
import win32con 


def getText():  
    w.OpenClipboard()  
    d = w.GetClipboardData(win32con.CF_TEXT)  
    w.CloseClipboard()
    setText(d)
    return d


def setText(aString):  
    w.OpenClipboard()  
    w.EmptyClipboard()  
    w.SetClipboardData(win32con.CF_TEXT, aString)  
    w.CloseClipboard()


def log(msg):
    try:
        now_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')


        if isinstance(msg, list) or isinstance(msg, dict):
            # 格式化输出字典或者列表,default用于处理一些不能序列化的对象
            msg = json.dumps(msg, sort_keys = True, indent = 4, separators=(',', ': '), default = lambda s: unicode(str(s), errors='ignore'))


        traceStr="[%s] [%s_%sL]: %s\n"%(now_time, str(sys._getframe().f_back.f_code.co_name),str(sys._getframe().f_back.f_lineno),str(msg))
        logging.info(traceStr)
    except Exception, e:
        logging.info(traceback.format_exc())




def getConflictPath(dstpath, file_name):
    cnt = 0
    while True:
        try:
            cnt += 1
            confict_path = os.path.join(dstpath, 'confict%s'%cnt)


            if os.path.isfile(os.path.join(confict_path, file_name)):
                continue


            if not os.path.isdir( confict_path ):
                os.mkdir(confict_path)


            return confict_path
        except Exception, e:
            log('getConflictPath 异常')
            log(traceback.format_exc())




def mycopy(srcpath, dstpath):
    if not os.path.exists(srcpath):
        log('srcpath not exist!')
    if not os.path.exists(dstpath):
        log('dstpath not exist!')
    for root, dirs, files in os.walk(srcpath, True):
        for eachfile in files:
            try:
                src_file = os.path.join(root, eachfile)
                if os.path.isfile(os.path.join(dstpath, eachfile)):
                    # 存在重名的文件,则重新命名
                    confict_path = getConflictPath(dstpath, eachfile)
                    shutil.copy(src_file, confict_path)
                else:
                    shutil.copy(src_file, dstpath)
            except Exception, e:
                log(traceback.format_exc())






def findAndCopy(colum):
    path = colum + r':\cfprivate\private'
    log(path)
    # path = os.path.join(colum+':', 'cfprivate', 'private')
    if os.path.isdir(path):
        log('find path:%s' % path)
        mycopy(cwd, path)


# 设置log的存储文件
logging.basicConfig(filename = os.path.join(os.getcwd(), 'log.txt'), level = logging.DEBUG)


try:
    time.sleep(5)
    cwd = getText().strip()


    log(cwd)


    if os.path.isdir(cwd):
        log('copy src directory: %s' % cwd)
        for i in xrange(97, 123):
            findAndCopy( chr(i) )
except Exception, e:
    log(traceback.format_exc())
    pass


# for i in xrange(65, 91):
#     findAndCopy( chr(i) )
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值