Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

可以支持批量重命名就好了 #103

Open
pheotry opened this issue Feb 20, 2023 · 2 comments
Open

可以支持批量重命名就好了 #103

pheotry opened this issue Feb 20, 2023 · 2 comments

Comments

@pheotry
Copy link

pheotry commented Feb 20, 2023

No description provided.

@wxy1343
Copy link
Owner

wxy1343 commented Feb 21, 2023

import functools
import threading
import time

from aliyunpan.api.type import FileInfo
from aliyunpan.cli import Commander

lock = threading.Lock()


def enum_files(commander: Commander, file_id, func):
    for path in commander.get_fid_list(file_id):  # 获取文件列表
        path: FileInfo
        if not path.type:  # 判断是否是文件
            enum_files(commander, path.id, func)  # 递归查询
        else:
            threading.Thread(target=func, args=(path,)).start()  # 多线程调用回调


def rename(commander: Commander, path: FileInfo):
    if path.type:  # 判断是否是文件
        name: str = path.name
        if name.endswith('.png'):  # 判断文件后缀为.png的文件
            while True:
                with lock:  # 加锁防止多线程打印内容混乱
                    print(name, end=' -> ')
                    name = name.rsplit('.')[0] + '.jpg'  # 修改后缀为.jpg
                    print(name)
                if path.id != commander.disk.update_file(path.id, name):  # 重命名文件
                    """这里是重命名失败的操作,有可能是由于网络问题或者接口限制"""
                    print('重命名失败:' + path.name)
                    time.sleep(0.5)
                    continue
                break


if __name__ == '__main__':
    c = Commander()
    enum_files(c, c.path_list.get_path_fid('test', update=False), functools.partial(rename, c))

@pheotry
Copy link
Author

pheotry commented Feb 23, 2023

import functools
import threading
import time

from aliyunpan.api.type import FileInfo
from aliyunpan.cli import Commander

lock = threading.Lock()


def enum_files(commander: Commander, file_id, func):
    for path in commander.get_fid_list(file_id):  # 获取文件列表
        path: FileInfo
        if not path.type:  # 判断是否是文件
            enum_files(commander, path.id, func)  # 递归查询
        else:
            threading.Thread(target=func, args=(path,)).start()  # 多线程调用回调


def rename(commander: Commander, path: FileInfo):
    if path.type:  # 判断是否是文件
        name: str = path.name
        if name.endswith('.png'):  # 判断文件后缀为.png的文件
            while True:
                with lock:  # 加锁防止多线程打印内容混乱
                    print(name, end=' -> ')
                    name = name.rsplit('.')[0] + '.jpg'  # 修改后缀为.jpg
                    print(name)
                if path.id != commander.disk.update_file(path.id, name):  # 重命名文件
                    """这里是重命名失败的操作,有可能是由于网络问题或者接口限制"""
                    print('重命名失败:' + path.name)
                    time.sleep(0.5)
                    continue
                break


if __name__ == '__main__':
    c = Commander()
    enum_files(c, c.path_list.get_path_fid('test', update=False), functools.partial(rename, c))

明白了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants