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) )
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) )
相关文章推荐
- Vim多文件操作及复制到系统剪贴板
- Python的shutil模块中文件的复制操作函数详解
- Python复制文件操作实例详解
- python文件操作_对文件进行复制拷贝_代码实现
- Python复制文件操作实例详解
- Python 文件/文件夹操作(读写read/write、复制copy、删除delete、重命名rename)
- paip.复制文件 文件操作 api的设计uapi java python php 最佳实践
- paip.复制文件 文件操作 api的设计uapi java python php 最佳实践
- 【Python/PE】python中实现文件操作的几种功能(删除指定目录下的所有内容,文件复制等)
- Python文件复制中如何复制相关文件的实际操作
- python文件操作--复制
- python 文件删除 移动 复制等操作
- Python 复制文件操作
- python文件操作--复制
- Python(一) 文件操作 复制
- Python的shutil模块中文件的复制操作函数详解
- 剪贴板操作——设置获取文本/UNICODE文本/HTML/BITMAP等等 监视剪贴板 保存BITMAP到文件
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- 使用Python进行稳定可靠的文件操作
- python3+PyQt5实现使用剪贴板做复制与粘帖