Open pheotry opened 1 year ago
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))
明白了