本地图片先压缩,再上传
叠1压缩
folder_zip_cut1_demo.py
import os
import shutil
import zipfile
import datetime# 源目录
source_dir = 'H:\\CCD叠一对齐度图片\\效果图\\'
# 日志文件路径
log_file_path = 'D:\\software\\dcs_log\\{}_folder_zip_cut1.log'.format(datetime.datetime.now().strftime('%Y-%m-%d'))# 记录开始时间
start_time = datetime.datetime.now()# 写入开始时间到日志文件
with open(log_file_path, 'a', encoding='utf-8') as log_file:log_file.write('开始时间: {}\n'.format(start_time))def get_today_str():today = datetime.datetime.now()today_str = today.strftime("%Y-%m-%d")return today_str# 遍历源目录下的所有文件和文件夹
for root, dirs, files in os.walk(source_dir):# 计算当前目录相对于源目录的层级level = root.replace(source_dir, '').count(os.sep)# 只处理5级子目录if level == 2:# 获取当前5级子目录的完整路径sub_dir_path = root# 压缩文件名(不包含路径)zip_file_name = os.path.basename(sub_dir_path) + '.zip'# 压缩文件完整路径(当前目录下)zip_file_path = os.path.join(os.path.dirname(sub_dir_path), zip_file_name)# 压缩当前5级子目录with zipfile.ZipFile(zip_file_path, 'w', zipfile.ZIP_DEFLATED) as zipf:for dirpath, dirnames, filenames in os.walk(sub_dir_path):if dirpath.find(get_today_str()) > 0:# print(f" 退出 dirpath {dirpath}")with open(log_file_path, 'a', encoding='utf-8') as log_file:log_file.write('退出 dirpath {}\n'.format(dirpath))continuefor filename in filenames:file_path = os.path.join(dirpath, filename)arcname = os.path.relpath(file_path, start=sub_dir_path)zipf.write(file_path, arcname)# 删除原始5级子目录shutil.rmtree(sub_dir_path)# 打印进度(可选)print(f'已压缩并删除: {sub_dir_path}')# 写入开始时间到日志文件# 上传时间upload_time = datetime.datetime.now()with open(log_file_path, 'a', encoding='utf-8') as log_file:log_file.write('已压缩并删除: {} {}\n'.format(sub_dir_path,upload_time))# 记录结束时间
end_time = datetime.datetime.now()# 写入结束时间到日志文件
with open(log_file_path, 'a', encoding='utf-8') as log_file:log_file.write('结束时间: {}\n'.format(end_time))print(f'日志已保存到: {log_file_path}')
叠2压缩
folder_zip_cut2_demo.py
import os
import shutil
import zipfile
import datetime# 源目录
source_dir = 'I:\\CCD叠二对齐度图片\\效果图\\'
# 日志文件路径
log_file_path = 'D:\\software\\dcs_log\\{}_folder_zip_cut2.log'.format(datetime.datetime.now().strftime('%Y-%m-%d'))# 记录开始时间
start_time = datetime.datetime.now()# 写入开始时间到日志文件
with open(log_file_path, 'a', encoding='utf-8') as log_file:log_file.write('开始时间: {}\n'.format(start_time))def get_today_str():today = datetime.datetime.now()today_str = today.strftime("%Y-%m-%d")return today_str# 遍历源目录下的所有文件和文件夹
for root, dirs, files in os.walk(source_dir):# 计算当前目录相对于源目录的层级level = root.replace(source_dir, '').count(os.sep)# 只处理5级子目录if level == 2:# 获取当前5级子目录的完整路径sub_dir_path = root# 压缩文件名(不包含路径)zip_file_name = os.path.basename(sub_dir_path) + '.zip'# 压缩文件完整路径(当前目录下)zip_file_path = os.path.join(os.path.dirname(sub_dir_path), zip_file_name)# 压缩当前5级子目录with zipfile.ZipFile(zip_file_path, 'w', zipfile.ZIP_DEFLATED) as zipf:for dirpath, dirnames, filenames in os.walk(sub_dir_path):if dirpath.find(get_today_str()) > 0:# print(f" 退出 dirpath {dirpath}")with open(log_file_path, 'a', encoding='utf-8') as log_file:log_file.write('退出 dirpath {}\n'.format(dirpath))continuefor filename in filenames:file_path = os.path.join(dirpath, filename)arcname = os.path.relpath(file_path, start=sub_dir_path)zipf.write(file_path, arcname)# 删除原始5级子目录shutil.rmtree(sub_dir_path)# 打印进度(可选)print(f'已压缩并删除: {sub_dir_path}')# 写入开始时间到日志文件# 上传时间upload_time = datetime.datetime.now()with open(log_file_path, 'a', encoding='utf-8') as log_file:log_file.write('已压缩并删除: {} {}\n'.format(sub_dir_path,upload_time))# 记录结束时间
end_time = datetime.datetime.now()# 写入结束时间到日志文件
with open(log_file_path, 'a', encoding='utf-8') as log_file:log_file.write('结束时间: {}\n'.format(end_time))print(f'日志已保存到: {log_file_path}')
叠3压缩
folder_zip_cut3_demo.py
import os
import shutil
import zipfile
import datetime# 源目录
source_dir = 'J:\\CCD叠三对齐度图片\\效果图\\'
# 日志文件路径
log_file_path = 'D:\\software\\dcs_log\\{}_folder_zip_cut3.log'.format(datetime.datetime.now().strftime('%Y-%m-%d'))# 记录开始时间
start_time = datetime.datetime.now()# 写入开始时间到日志文件
with open(log_file_path, 'a', encoding='utf-8') as log_file:log_file.write('开始时间: {}\n'.format(start_time))def get_today_str():today = datetime.datetime.now()today_str = today.strftime("%Y-%m-%d")return today_str# 遍历源目录下的所有文件和文件夹
for root, dirs, files in os.walk(source_dir):# 计算当前目录相对于源目录的层级level = root.replace(source_dir, '').count(os.sep)# 只处理5级子目录if level == 2:# 获取当前5级子目录的完整路径sub_dir_path = root# 压缩文件名(不包含路径)zip_file_name = os.path.basename(sub_dir_path) + '.zip'# 压缩文件完整路径(当前目录下)zip_file_path = os.path.join(os.path.dirname(sub_dir_path), zip_file_name)# 压缩当前5级子目录with zipfile.ZipFile(zip_file_path, 'w', zipfile.ZIP_DEFLATED) as zipf:for dirpath, dirnames, filenames in os.walk(sub_dir_path):if dirpath.find(get_today_str()) > 0:# print(f" 退出 dirpath {dirpath}")with open(log_file_path, 'a', encoding='utf-8') as log_file:log_file.write('退出 dirpath {}\n'.format(dirpath))continuefor filename in filenames:file_path = os.path.join(dirpath, filename)arcname = os.path.relpath(file_path, start=sub_dir_path)zipf.write(file_path, arcname)# 删除原始5级子目录shutil.rmtree(sub_dir_path)# 打印进度(可选)print(f'已压缩并删除: {sub_dir_path}')# 写入开始时间到日志文件# 上传时间upload_time = datetime.datetime.now()with open(log_file_path, 'a', encoding='utf-8') as log_file:log_file.write('已压缩并删除: {} {}\n'.format(sub_dir_path,upload_time))# 记录结束时间
end_time = datetime.datetime.now()# 写入结束时间到日志文件
with open(log_file_path, 'a', encoding='utf-8') as log_file:log_file.write('结束时间: {}\n'.format(end_time))print(f'日志已保存到: {log_file_path}')
叠1 文件上传
zip_folder_upload_delete_move_cut1.py
"""
遍历目录文件夹I:\CCD叠二对齐度图片\效果图test\下5级子目录的zip文件,上传到FTP服务器
1.FTP服务器设置如下ftp_server = 'nas01.hzccd.svolt.cn'ftp_username = 'C11QD'ftp_password = 'Svolt@2023'
1.ftp路径对应于路径 “/1901/C11/赢合/6#/test18/”+本地压缩文件的相对路径,路径不存在则创建,用python实现上传逻辑,
2.对每个压缩文件上传的开始时间,结束时间,输出日志到D:\software\dcs_log ,日志文件格式:日期+cut2_ftp_zip_folder.log
3.上传成功的文件备份到I:\已上传_CCD叠二对齐度图片\效果图test\+源文件的相对路径,然后再把上传成功的文件删除报错:文件 xxx.zip 上传失败: 550 [2: No such file or directory] error calling getattr API.
"""import os
import ftplib
from datetime import datetimeredis_host = "localhost"
redis_port = 6379
redis_db = 9
import redis
r = redis.StrictRedis(host=redis_host, port=redis_port, db=redis_db, decode_responses=True)def get_num_from_equipment_code(equipment_code):equipment_code_len = len(equipment_code)str_myneed = equipment_code[equipment_code_len-2:]int_myneed = int(str_myneed)str_myneed = str(int_myneed)str_myneed += "#"return str_myneedEquipmentCode = r.get("EquipmentCode_lineEdit")
EquipmentCode_num = get_num_from_equipment_code(EquipmentCode)# FTP服务器设置
ftp_server = 'nas01.hzccd.svolt.cn'
ftp_username = 'C11QD'
ftp_password = 'Svolt@2023'
ftp_base_path = '/1901/C11/赢合/"+EquipmentCode_num+"/Image_zip/CCD叠一对齐度图片/效果图/'# 本地目录设置
local_base_dir = 'H:\\CCD叠一对齐度图片\\效果图\\'
log_dir = 'D:\\software\\dcs_log\\'
backup_dir = 'H:\\已上传_CCD叠一对齐度图片\\效果图\\'# 创建日志文件路径
today = datetime.now().strftime('%Y-%m-%d')
log_file_path = os.path.join(log_dir, f'{today}_cut1_zip_folder_upload_and_move_and_del.log')# 确保日志文件目录存在
if not os.path.exists(log_dir):os.makedirs(log_dir)def create_remote_dir(ftp, remote_path):# 递归创建远程目录dirs = remote_path.split('/')current_remote_dir = ''for dir_name in dirs:if dir_name: # 避免末尾的空字符串current_remote_dir += '/' + dir_nametry:ftp.mkd(current_remote_dir)except ftplib.all_errors as e:if "550" not in str(e): # 如果不是目录已存在的错误,则抛出异常raise# 遍历本地目录中的压缩文件(包括5级子目录)
def traverse_and_upload(root_dir, ftp_path):for dirpath, dirnames, filenames in os.walk(root_dir):# 只遍历到5级子目录if len(dirpath.split('\\')) - len(root_dir.split('\\')) > 4:continuefor filename in filenames:if filename.endswith('.zip'):local_file_path = os.path.join(dirpath, filename)rel_path = os.path.relpath(dirpath, local_base_dir)ftp_full_path = ftp_path + rel_path + '/' + filenameftp_full_path = ftp_full_path.replace("\\", "/")# 写入日志with open(log_file_path, 'a', encoding='utf-8') as log_file:log_file.write(f'local_file_path {local_file_path} \n')log_file.write(f'rel_path {rel_path} \n')log_file.write(f'ftp_full_path {ftp_full_path} \n')try:with ftplib.FTP(ftp_server, ftp_username, ftp_password) as ftp:ftp.encoding = "gb2312"# 切换到被动模式(如果需要)ftp.set_pasv(True)# 创建FTP目录(如果不存在)remote_dir = ftp_full_path.rsplit('/', 1)[0]remote_dir = remote_dir.replace("\\", "/")create_remote_dir(ftp, remote_dir)# 写入日志with open(log_file_path, 'a', encoding='utf-8') as log_file:log_file.write(f'remote_dir {remote_dir} \n')# if not any(dir for dir in ftp.nlst(remote_dir) if ftp.voidcmd(f'CWD {dir}') == 250):# ftp.mkd(remote_dir)# 开始上传文件并记录时间start_time = datetime.now()with open(local_file_path, 'rb') as f:ftp.storbinary(f'STOR {ftp_full_path}', f)end_time = datetime.now()# 写入日志with open(log_file_path, 'a', encoding='utf-8') as log_file:log_file.write(f'文件 {filename} 上传成功,开始时间: {start_time}, 结束时间: {end_time}\n')# 备份并删除文件backup_path = os.path.join(backup_dir, rel_path, filename)os.makedirs(os.path.dirname(backup_path), exist_ok=True)os.replace(local_file_path, backup_path)except ftplib.all_errors as e:# 写入FTP错误到日志with open(log_file_path, 'a', encoding='utf-8') as log_file:log_file.write(f'文件 {filename} 上传失败: {e}\n')except Exception as e:# 写入通用错误到日志with open(log_file_path, 'a', encoding='utf-8') as log_file:log_file.write(f'处理文件 {filename} 时发生未知错误: {e}\n')# 开始遍历和上传traverse_and_upload(local_base_dir, ftp_base_path)print('处理完成。')
叠2 文件上传
zip_folder_upload_delete_move_cut2.py
"""
遍历目录文件夹I:\CCD叠二对齐度图片\效果图test\下5级子目录的zip文件,上传到FTP服务器
1.FTP服务器设置如下ftp_server = 'nas01.hzccd.svolt.cn'ftp_username = 'C11QD'ftp_password = 'Svolt@2023'
1.ftp路径对应于路径 “/1901/C11/赢合/6#/test18/”+本地压缩文件的相对路径,路径不存在则创建,用python实现上传逻辑,
2.对每个压缩文件上传的开始时间,结束时间,输出日志到D:\software\dcs_log ,日志文件格式:日期+cut2_ftp_zip_folder.log
3.上传成功的文件备份到I:\已上传_CCD叠二对齐度图片\效果图test\+源文件的相对路径,然后再把上传成功的文件删除报错:文件 xxx.zip 上传失败: 550 [2: No such file or directory] error calling getattr API.
"""import os
import ftplib
from datetime import datetimeredis_host = "localhost"
redis_port = 6379
redis_db = 9
import redis
r = redis.StrictRedis(host=redis_host, port=redis_port, db=redis_db, decode_responses=True)def get_num_from_equipment_code(equipment_code):equipment_code_len = len(equipment_code)str_myneed = equipment_code[equipment_code_len-2:]int_myneed = int(str_myneed)str_myneed = str(int_myneed)str_myneed += "#"return str_myneedEquipmentCode = r.get("EquipmentCode_lineEdit")
EquipmentCode_num = get_num_from_equipment_code(EquipmentCode)# FTP服务器设置
ftp_server = 'nas01.hzccd.svolt.cn'
ftp_username = 'C11QD'
ftp_password = 'Svolt@2023'
ftp_base_path = '/1901/C11/赢合/"+EquipmentCode_num+"/Image_zip/CCD叠二对齐度图片/效果图/'# 本地目录设置
local_base_dir = 'I:\\CCD叠二对齐度图片\\效果图\\'
log_dir = 'D:\\software\\dcs_log\\'
backup_dir = 'I:\\已上传_CCD叠二对齐度图片\\效果图\\'# 创建日志文件路径
today = datetime.now().strftime('%Y-%m-%d')
log_file_path = os.path.join(log_dir, f'{today}_cut2_zip_folder_upload_and_move_and_del.log')# 确保日志文件目录存在
if not os.path.exists(log_dir):os.makedirs(log_dir)def create_remote_dir(ftp, remote_path):# 递归创建远程目录dirs = remote_path.split('/')current_remote_dir = ''for dir_name in dirs:if dir_name: # 避免末尾的空字符串current_remote_dir += '/' + dir_nametry:ftp.mkd(current_remote_dir)except ftplib.all_errors as e:if "550" not in str(e): # 如果不是目录已存在的错误,则抛出异常raise# 遍历本地目录中的压缩文件(包括5级子目录)
def traverse_and_upload(root_dir, ftp_path):for dirpath, dirnames, filenames in os.walk(root_dir):# 只遍历到5级子目录if len(dirpath.split('\\')) - len(root_dir.split('\\')) > 4:continuefor filename in filenames:if filename.endswith('.zip'):local_file_path = os.path.join(dirpath, filename)rel_path = os.path.relpath(dirpath, local_base_dir)ftp_full_path = ftp_path + rel_path + '/' + filenameftp_full_path = ftp_full_path.replace("\\", "/")# 写入日志with open(log_file_path, 'a', encoding='utf-8') as log_file:log_file.write(f'local_file_path {local_file_path} \n')log_file.write(f'rel_path {rel_path} \n')log_file.write(f'ftp_full_path {ftp_full_path} \n')try:with ftplib.FTP(ftp_server, ftp_username, ftp_password) as ftp:ftp.encoding = "gb2312"# 切换到被动模式(如果需要)ftp.set_pasv(True)# 创建FTP目录(如果不存在)remote_dir = ftp_full_path.rsplit('/', 1)[0]remote_dir = remote_dir.replace("\\", "/")create_remote_dir(ftp, remote_dir)# 写入日志with open(log_file_path, 'a', encoding='utf-8') as log_file:log_file.write(f'remote_dir {remote_dir} \n')# if not any(dir for dir in ftp.nlst(remote_dir) if ftp.voidcmd(f'CWD {dir}') == 250):# ftp.mkd(remote_dir)# 开始上传文件并记录时间start_time = datetime.now()with open(local_file_path, 'rb') as f:ftp.storbinary(f'STOR {ftp_full_path}', f)end_time = datetime.now()# 写入日志with open(log_file_path, 'a', encoding='utf-8') as log_file:log_file.write(f'文件 {filename} 上传成功,开始时间: {start_time}, 结束时间: {end_time}\n')# 备份并删除文件backup_path = os.path.join(backup_dir, rel_path, filename)os.makedirs(os.path.dirname(backup_path), exist_ok=True)os.replace(local_file_path, backup_path)except ftplib.all_errors as e:# 写入FTP错误到日志with open(log_file_path, 'a', encoding='utf-8') as log_file:log_file.write(f'文件 {filename} 上传失败: {e}\n')except Exception as e:# 写入通用错误到日志with open(log_file_path, 'a', encoding='utf-8') as log_file:log_file.write(f'处理文件 {filename} 时发生未知错误: {e}\n')# 开始遍历和上传traverse_and_upload(local_base_dir, ftp_base_path)print('处理完成。')
叠3 文件上传
zip_folder_upload_delete_move_cut3.py
"""
遍历目录文件夹I:\CCD叠二对齐度图片\效果图test\下5级子目录的zip文件,上传到FTP服务器
1.FTP服务器设置如下ftp_server = 'nas01.hzccd.svolt.cn'ftp_username = 'C11QD'ftp_password = 'Svolt@2023'
1.ftp路径对应于路径 “/1901/C11/赢合/6#/test18/”+本地压缩文件的相对路径,路径不存在则创建,用python实现上传逻辑,
2.对每个压缩文件上传的开始时间,结束时间,输出日志到D:\software\dcs_log ,日志文件格式:日期+cut2_ftp_zip_folder.log
3.上传成功的文件备份到I:\已上传_CCD叠二对齐度图片\效果图test\+源文件的相对路径,然后再把上传成功的文件删除报错:文件 xxx.zip 上传失败: 550 [2: No such file or directory] error calling getattr API.
"""import os
import ftplib
from datetime import datetimeredis_host = "localhost"
redis_port = 6379
redis_db = 9
import redis
r = redis.StrictRedis(host=redis_host, port=redis_port, db=redis_db, decode_responses=True)def get_num_from_equipment_code(equipment_code):equipment_code_len = len(equipment_code)str_myneed = equipment_code[equipment_code_len-2:]int_myneed = int(str_myneed)str_myneed = str(int_myneed)str_myneed += "#"return str_myneedEquipmentCode = r.get("EquipmentCode_lineEdit")
EquipmentCode_num = get_num_from_equipment_code(EquipmentCode)# FTP服务器设置
ftp_server = 'nas01.hzccd.svolt.cn'
ftp_username = 'C11QD'
ftp_password = 'Svolt@2023'
ftp_base_path = '/1901/C11/赢合/"+EquipmentCode_num+"/Image_zip/CCD叠三对齐度图片/效果图/'# 本地目录设置
local_base_dir = 'J:\\CCD叠三对齐度图片\\效果图\\'
log_dir = 'D:\\software\\dcs_log\\'
backup_dir = 'J:\\已上传_CCD叠三对齐度图片\\效果图\\'# 创建日志文件路径
today = datetime.now().strftime('%Y-%m-%d')
log_file_path = os.path.join(log_dir, f'{today}_cut3_zip_folder_upload_and_move_and_del.log')# 确保日志文件目录存在
if not os.path.exists(log_dir):os.makedirs(log_dir)def create_remote_dir(ftp, remote_path):# 递归创建远程目录dirs = remote_path.split('/')current_remote_dir = ''for dir_name in dirs:if dir_name: # 避免末尾的空字符串current_remote_dir += '/' + dir_nametry:ftp.mkd(current_remote_dir)except ftplib.all_errors as e:if "550" not in str(e): # 如果不是目录已存在的错误,则抛出异常raise# 遍历本地目录中的压缩文件(包括5级子目录)
def traverse_and_upload(root_dir, ftp_path):for dirpath, dirnames, filenames in os.walk(root_dir):# 只遍历到5级子目录if len(dirpath.split('\\')) - len(root_dir.split('\\')) > 4:continuefor filename in filenames:if filename.endswith('.zip'):local_file_path = os.path.join(dirpath, filename)rel_path = os.path.relpath(dirpath, local_base_dir)ftp_full_path = ftp_path + rel_path + '/' + filenameftp_full_path = ftp_full_path.replace("\\", "/")# 写入日志with open(log_file_path, 'a', encoding='utf-8') as log_file:log_file.write(f'local_file_path {local_file_path} \n')log_file.write(f'rel_path {rel_path} \n')log_file.write(f'ftp_full_path {ftp_full_path} \n')try:with ftplib.FTP(ftp_server, ftp_username, ftp_password) as ftp:ftp.encoding = "gb2312"# 切换到被动模式(如果需要)ftp.set_pasv(True)# 创建FTP目录(如果不存在)remote_dir = ftp_full_path.rsplit('/', 1)[0]remote_dir = remote_dir.replace("\\", "/")create_remote_dir(ftp, remote_dir)# 写入日志with open(log_file_path, 'a', encoding='utf-8') as log_file:log_file.write(f'remote_dir {remote_dir} \n')# if not any(dir for dir in ftp.nlst(remote_dir) if ftp.voidcmd(f'CWD {dir}') == 250):# ftp.mkd(remote_dir)# 开始上传文件并记录时间start_time = datetime.now()with open(local_file_path, 'rb') as f:ftp.storbinary(f'STOR {ftp_full_path}', f)end_time = datetime.now()# 写入日志with open(log_file_path, 'a', encoding='utf-8') as log_file:log_file.write(f'文件 {filename} 上传成功,开始时间: {start_time}, 结束时间: {end_time}\n')# 备份并删除文件backup_path = os.path.join(backup_dir, rel_path, filename)os.makedirs(os.path.dirname(backup_path), exist_ok=True)os.replace(local_file_path, backup_path)except ftplib.all_errors as e:# 写入FTP错误到日志with open(log_file_path, 'a', encoding='utf-8') as log_file:log_file.write(f'文件 {filename} 上传失败: {e}\n')except Exception as e:# 写入通用错误到日志with open(log_file_path, 'a', encoding='utf-8') as log_file:log_file.write(f'处理文件 {filename} 时发生未知错误: {e}\n')# 开始遍历和上传traverse_and_upload(local_base_dir, ftp_base_path)print('处理完成。')