files_fetching.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import argparse
  2. import os
  3. import shutil
  4. import concurrent.futures
  5. import random
  6. version = "1.0.0"
  7. def is_in_whitelist(file_name, whitelist):
  8. # 获取文件的后缀名
  9. _, ext = os.path.splitext(file_name)
  10. # 判断后缀名是否在白名单中
  11. return ext.lower() in whitelist
  12. def list_files(directory, whitelist, blacklist):
  13. # 遍历目录中的文件和文件夹
  14. res = []
  15. for root, dirs, files in os.walk(directory):
  16. # 只输出文件名,不包括文件夹
  17. for file in files:
  18. if not file.startswith(".") and is_in_whitelist(file, whitelist):
  19. file_path = os.path.join(root, file)
  20. res.append(file_path)
  21. return res
  22. def check_path(path_to_check):
  23. try:
  24. if not os.path.exists(path_to_check):
  25. os.makedirs(path_to_check)
  26. except OSError as e:
  27. print(f"发生错误: {e}")
  28. def copy_file(src_file, dst_file):
  29. if os.path.exists(dst_file):
  30. print(f"文件 {dst_file} 已存在")
  31. else:
  32. try:
  33. shutil.copy(src_file, dst_file)
  34. print(f"文件已成功从 {src_file} 拷贝到 {dst_file}")
  35. except FileNotFoundError:
  36. print(f"源文件 {src_file} 不存在")
  37. except PermissionError:
  38. print(f"没有权限拷贝文件到 {dst_file}")
  39. except Exception as e:
  40. print(f"发生错误: {e}")
  41. def copy_files_concurrently(src_files, dst_dir):
  42. with concurrent.futures.ThreadPoolExecutor() as executor:
  43. futures = [executor.submit(copy_file, src, os.path.join(dst_dir, os.path.basename(src))) for src in src_files]
  44. for future in concurrent.futures.as_completed(futures):
  45. try:
  46. # 你可以在这里处理返回的结果(如果有的话),但在这个例子中,copy_file没有返回值
  47. pass
  48. except Exception as exc:
  49. print(f'Generated an exception: {exc}')
  50. def main():
  51. parse = argparse.ArgumentParser("可以随机抓取指定目录下的文件,可指定后缀名,指定要抓取的数量\n")
  52. parse.add_argument("-v", "--version", action="version", version=version)
  53. parse.add_argument("input", help="输入路径")
  54. parse.add_argument("output", help="输出路径")
  55. parse.add_argument("--count", type=int, help="随机抓取的文件数量,不设置则抓取所有文件")
  56. parse.add_argument("--whitelist", nargs='+', help="文件后缀名白名单,如果设置白名单,则只会选取白名单中的文件格式")
  57. parse.add_argument("--blacklist", nargs='+', help="[未实装]文件后缀名黑名单,如果设置黑名单,则会选取过滤黑名单中的文件格式")
  58. args = parse.parse_args()
  59. whitelist = []
  60. blacklist = []
  61. count = -1
  62. if args.whitelist:
  63. for suffix in args.whitelist:
  64. if not suffix.startswith("."):
  65. whitelist.append("." + suffix)
  66. else:
  67. whitelist.append(suffix)
  68. if args.blacklist:
  69. for suffix in args.blacklist:
  70. if not suffix.startswith("."):
  71. blacklist.append("." + suffix)
  72. else:
  73. blacklist.append(suffix)
  74. if args.count:
  75. count = args.count
  76. files = list_files(args.input, whitelist, blacklist)
  77. files_to_copy = []
  78. check_path(args.output)
  79. if count == -1 or len(files) <= count:
  80. # 拷贝全部
  81. files_to_copy = files
  82. else:
  83. files_to_copy = random.sample(files, count)
  84. copy_files_concurrently(files_to_copy, args.output)
  85. if __name__ == "__main__":
  86. main()