侧边栏壁纸
博主头像
YOUZI

我依旧在追寻自由的路上

  • 累计撰写 85 篇文章
  • 累计创建 10 个分类
  • 累计创建 27 个标签

目 录CONTENT

文章目录

命名后上传至阿里云盘

柚子
原创 / 2024-07-22 / 0 评论 / 0 点赞 / 23 阅读 / 0 字
温馨提示:
本文最后更新于59天前,若内容或图片失效,请留言反馈。 部分素材来自网络,若不小心影响您的利益,请联系 站长 删除。

代码

import os
import json
import shutil
from aligo import Aligo



def get_current_count(prefix):
    """
    获取当前计数器的值。

    该函数用于读取记录文件中特定前缀的计数器值,如果不存在,则返回0。

    :param prefix: 文件名前缀
    :return: 计数器值
    """
    if os.path.exists(record_file):
        with open(record_file, 'r') as f:
            data = json.load(f)
            return data.get(prefix, 0)
    return 0


def update_current_count(prefix, count):
    """
    更新当前计数器的值。

    该函数用于将特定前缀的计数器值更新到记录文件中。如果记录文件不存在,则创建新文件。

    :param prefix: 文件名前缀
    :param count: 新的计数器值
    """
    if os.path.exists(record_file):
        with open(record_file, 'r') as f:
            data = json.load(f)
    else:
        data = {}
    data[prefix] = count
    with open(record_file, 'w') as f:
        json.dump(data, f)


def rename_files(directory, prefix, extensions):
    """
    重命名指定目录下的文件。

    该函数根据指定的前缀和文件扩展名,对目录下的文件进行重命名。重命名规则为前缀-三位数字后缀,数字从记录文件中读取并更新。

    :param directory: 需要重命名文件的目录
    :param prefix: 文件名前缀
    :param extensions: 需要重命名的文件扩展名列表
    """
    # 列出目录下所有文件,并筛选出符合指定扩展名的文件
    files = [f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f))]
    current_count = get_current_count(prefix)

    for file in files:
        ext = os.path.splitext(file)[1]
        if ext.lower() in extensions:
            current_count += 1
            # 根据前缀和计数器生成新文件名
            new_name = f"{prefix}{current_count:03d}{ext}" if prefix else f"{current_count:05d}{ext}"
            old_path = os.path.join(directory, file)
            new_path = os.path.join(directory, new_name)
            os.rename(old_path, new_path)
            print(f"重命名: {file} -> {new_name}")

    # 更新记录文件中的计数器值
    update_current_count(prefix, current_count)


def move_files(destination_directory, directory):
    if not os.path.exists(destination_directory):
        os.makedirs(destination_directory)

    for file in os.listdir(directory):
        file_path = os.path.join(directory, file)
        if os.path.isfile(file_path):
            shutil.move(file_path, destination_directory)
            print(f"文件移动: {file_path} -> {destination_directory}")


def upload_to_aliyunpan(directory, aliyun_folder_path):
    ali = Aligo()

    # 确保创建完整的目录结构
    current_folder_id = 'root'
    for folder in aliyun_folder_path.split('/'):
        if folder:
            target_folder = ali.get_folder_by_path(folder, parent_file_id=current_folder_id)
            if target_folder is None:
                target_folder = ali.create_folder(name=folder, parent_file_id=current_folder_id)
            current_folder_id = target_folder.file_id

    for file in os.listdir(directory):
        file_path = os.path.join(directory, file)
        if os.path.isfile(file_path):
            ali.upload_file(file_path, parent_file_id=current_folder_id)
            print(f"文件上传至阿里云盘文件夹 '{aliyun_folder_path}': {file_path}")

# json文件命名
record_file = 'rename_record_aliyun.json'

# 指定json文件、需要重命名文件的目录、文件扩展名
directory_path = r"E:\webp\temp"
prefix_name = 'pixiv-'  # 前缀可为任意字符串
extensions_map = ['.png', '.jpg', '.webp']
rename_files(directory_path, prefix_name, extensions_map)

# 移动处理后的目录至另一个指定目录
move_directory = r"E:\webp\move_files"
move_files(move_directory, directory_path)

# 替换阿里云盘文件夹路径中的test为prefix_name中的firefly
aliyun_path = 'photos/directory'
aliyun_path = aliyun_path.replace('directory', prefix_name.rstrip('-'))  # 去除前缀中的 '-' 并替换

# 上传move_directory中的文件至指定的阿里云盘文件夹
upload_to_aliyunpan(move_directory, aliyun_path)

0

评论区