extract_data.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import argparse
  2. import os
  3. import shutil
  4. import tarfile
  5. def run():
  6. parser = argparse.ArgumentParser(description='Extract')
  7. parser.add_argument("--source", type=str)
  8. parser.add_argument('--target', type=str)
  9. parser.add_argument('--task', type=str)
  10. args = parser.parse_args()
  11. files = os.listdir(args.source)
  12. for file in files:
  13. if args.task in file:
  14. print(f"Processing {file}")
  15. try:
  16. source_path = os.path.join(args.source, file)
  17. target_path = os.path.join(args.target, args.task)
  18. file_obj = tarfile.open(source_path, "r")
  19. file_obj.extractall(target_path)
  20. file_obj.close()
  21. old_name = os.path.join(target_path, args.task)
  22. place = file.replace(args.task, "").replace("_.tar", "")
  23. new_name = os.path.join(target_path, place)
  24. shutil.move(old_name, new_name)
  25. print(f"Extracted {file}")
  26. except Exception as e:
  27. print()
  28. print(f"Failed to extract {file}")
  29. print(e)
  30. print()
  31. if __name__ == '__main__':
  32. run()