12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- import os
- def get_files_to_upload(local_directory, remote_directory, depth, file_extension='*'):
- """
- 获取目录下文件并转化为目标目录地址
- :param local_directory: 本地目录
- :param remote_directory: 远程目录
- :param depth: 读取深度,-1为全部
- :param file_extension: 文件后缀
- :return:
- """
- files_to_upload = []
- for root, dirs, files in os.walk(local_directory):
- if depth >= 0:
- # 计算当前目录的深度
- current_depth = root[len(local_directory) + len(os.sep):].count(os.sep)
- if current_depth > depth:
- continue
- destination_dir = os.path.join(remote_directory, os.path.relpath(root, local_directory))
- for file in files:
- if file_extension == '*' or file.endswith(file_extension):
- local_path = os.path.join(root, file) # 本地文件路径
- remote_path = os.path.join(destination_dir, file) # 远程文件路径
- remote_path = remote_path.replace('\\','/')
- files_to_upload.append({
- 'local_path': local_path,
- 'remote_path': remote_path
- })
- return files_to_upload
- def example():
- local_directory = "D:\\Download\\downloads" # 本地目录路径
- remote_directory = "/home/ai/work/project/stable-diffusion-webui/extensions/sd-webui-controlnet/downloads" # 远程目录路径
- depth = -1 # 包括多少级子目录下的文件(-1代表包括所有子目录)
- files_to_upload = get_files_to_upload(local_directory, remote_directory, depth)
- print(files_to_upload)
- # Example usage:
- if __name__ == '__main__':
- example()
|