remove_downloads.py 1021 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. """Remove downloads for a particular date.
  2. Files are saved in a folder named after the current date,
  3. for example downloaded_data/farm_gate_agriculture_energy/2024-11-07
  4. This script deletes all files in such a folder. It is
  5. useful when testing downloads. Needs to be updated with the directory
  6. structure or maybe can be deleted altogether later.
  7. """
  8. import os
  9. import click
  10. from faostat_data_primap.helper.definitions import downloaded_data_path
  11. @click.command()
  12. @click.option(
  13. "--date",
  14. help="The day on which the data to be deleted was downloaded",
  15. default="2023-11-09",
  16. )
  17. def run(date: str):
  18. """
  19. Delete all downloaded files for one day.
  20. """
  21. domains = os.listdir(downloaded_data_path)
  22. for domain in domains:
  23. path_to_files = downloaded_data_path / domain / date
  24. files_to_delete = os.listdir(path_to_files)
  25. for file in files_to_delete:
  26. path_to_file = path_to_files / file
  27. os.remove(path_to_file)
  28. if __name__ == "__main__":
  29. run()