file.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import os
  2. def get_files_to_upload(local_directory, remote_directory, depth, file_extension='*'):
  3. """
  4. 获取目录下文件并转化为目标目录地址
  5. :param local_directory: 本地目录
  6. :param remote_directory: 远程目录
  7. :param depth: 读取深度,-1为全部
  8. :param file_extension: 文件后缀
  9. :return:
  10. """
  11. files_to_upload = []
  12. for root, dirs, files in os.walk(local_directory):
  13. if depth >= 0:
  14. # 计算当前目录的深度
  15. current_depth = root[len(local_directory) + len(os.sep):].count(os.sep)
  16. if current_depth > depth:
  17. continue
  18. destination_dir = os.path.join(remote_directory, os.path.relpath(root, local_directory))
  19. for file in files:
  20. if file_extension == '*' or file.endswith(file_extension):
  21. local_path = os.path.join(root, file) # 本地文件路径
  22. remote_path = os.path.join(destination_dir, file) # 远程文件路径
  23. remote_path = remote_path.replace('\\','/')
  24. files_to_upload.append({
  25. 'local_path': local_path,
  26. 'remote_path': remote_path
  27. })
  28. return files_to_upload
  29. def example():
  30. local_directory = "D:\\Download\\downloads" # 本地目录路径
  31. remote_directory = "/home/ai/work/project/stable-diffusion-webui/extensions/sd-webui-controlnet/downloads" # 远程目录路径
  32. depth = -1 # 包括多少级子目录下的文件(-1代表包括所有子目录)
  33. files_to_upload = get_files_to_upload(local_directory, remote_directory, depth)
  34. print(files_to_upload)
  35. # Example usage:
  36. if __name__ == '__main__':
  37. example()