1.按照每个文件夹文件总大小不超过25M,文件总数量不超过25个进行分割
import os
import shutil
def split_files(source_folder, target_folder, max_files_per_folder=25, max_size_per_folder=25):
#kindle限制35M,但因为邮件发送的时候转base64,文件大小会增加到4/3左右,所以限制大小25M保险一些.
# Create target folder if it doesn't exist
if not os.path.exists(target_folder):
os.makedirs(target_folder)
# Initialize counters
folder_count = 0
folder_size = 0
file_count = 0
folder_path = os.path.join(target_folder, f'Folder_{folder_count}')
os.makedirs(folder_path)
# Iterate over files in source folder
for file_name in os.listdir(source_folder):
if file_name.endswith('.txt'):
file_path = os.path.join(source_folder, file_name)
file_size = os.path.getsize(file_path)
# Check if adding this file would exceed the maximum size for this folder
if folder_size + file_size > max_size_per_folder * 1024 * 1024:
# If so, create a new folder and reset the counters
folder_count += 1
folder_path = os.path.join(target_folder, f'Folder_{folder_count}')
os.makedirs(folder_path)
folder_size = 0
file_count = 0
# Check if this folder has reached the maximum number of files
if file_count >= max_files_per_folder:
# If so, create a new folder and reset the counters
folder_count += 1
folder_path = os.path.join(target_folder, f'Folder_{folder_count}')
os.makedirs(folder_path)
folder_size = 0
file_count = 0
# Move the file to the current folder
shutil.move(file_path, folder_path)
file_count += 1
folder_size += file_size
print(f'Successfully split {len(os.listdir(source_folder))} files into {folder_count} folders.')
# Example usage:
split_files('/Users/xxx/Downloads/kindlepath/source', '/Users/xxx/Downloads/kindlepath/target')
2.对每个子文件夹进行邮件推送
import os
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
smtp_server = 'smtp.qq.com' # 邮件服务器地址
smtp_port = 587 # 邮件服务器端口号
sender = 'xxx' # 发送者邮箱
password = 'xxx' # 发送者邮箱密码
receiver = 'xxx@kindle.com' # 接收者邮箱
# 指定要遍历的文件夹路径
folder_path = '/Users/xxx/Downloads/kindlepath/target/'
# 获取指定文件夹中的所有子文件夹地址
subfolders = [os.path.join(folder_path, item) for item in os.listdir(folder_path) if os.path.isdir(os.path.join(folder_path, item))]
print(subfolders)
for folder_path in subfolders:
print(folder_path)
subjectname = os.path.basename(folder_path)
print(subjectname)
# 构建邮件对象
msg = MIMEMultipart()
msg['Subject'] = subjectname # 邮件主题
msg['From'] = sender # 发送者邮箱
msg['To'] = receiver # 接收者邮箱
# 遍历文件夹,添加所有txt文件作为附件
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith('.txt'):
file_path = os.path.join(root, file)
with open(file_path, 'rb') as f:
attachment = MIMEApplication(f.read(), _subtype='txt')
attachment.add_header('Content-Disposition', 'attachment', filename=file)
msg.attach(attachment)
# 发送邮件
with smtplib.SMTP(smtp_server, smtp_port) as smtp:
smtp.ehlo() # 向邮箱发送SMTP 'ehlo' 命令
smtp.starttls()
smtp.login(sender, password)
smtp.sendmail(sender, receiver, msg.as_string())
print('邮件发送成功')
print('所有邮件发送成功')
实际发送的时候,可能个别txt会发送失败,kindle方会发邮件通知失败文件名。
参考资料:
chatGPT 3.5