remove_downloads.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. # "--level",
  14. # help="Delete all files on domain or release level",
  15. # default="domain",
  16. # )
  17. def run():
  18. """
  19. Delete all downloaded files for all domains and all releases
  20. """
  21. domains = [
  22. d
  23. for d in os.listdir(downloaded_data_path)
  24. if os.path.isdir(downloaded_data_path / d)
  25. ]
  26. for domain in domains:
  27. path_to_releases = downloaded_data_path / domain
  28. releases = [
  29. d
  30. for d in os.listdir(path_to_releases)
  31. if os.path.isdir(path_to_releases / d)
  32. ]
  33. for release in releases:
  34. path_to_files = downloaded_data_path / domain / release
  35. files_to_delete = os.listdir(path_to_files)
  36. for file in files_to_delete:
  37. path_to_file = path_to_files / file
  38. os.remove(path_to_file)
  39. if __name__ == "__main__":
  40. run()