您的位置:首页 > 编程语言 > Python开发

python 剪贴板与文件复制操作

2013-10-14 11:07 274 查看
#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) )
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: