file_rename.py 1018 B

123456789101112131415161718192021222324252627
  1. import csv
  2. from datetime import datetime
  3. from pathlib import Path
  4. base_dir = "C:/Projekte/Python"
  5. # timestamp = date.today().strftime('%Y%m%d')
  6. def main():
  7. csv_reader = csv.reader(open(base_dir + "/gctools/DSG_Uebersetzung.csv", "r", encoding="latin-1"), delimiter=";")
  8. translation = dict([(row[0], row[1]) for row in csv_reader if row[1] != ""])
  9. target = Path(base_dir + "/export_dsg")
  10. for filename in Path(base_dir + "/export").glob("*"):
  11. for old, new in translation.items():
  12. if "_" + old in filename.name:
  13. new_name = filename.name.replace(old, new)
  14. new_timestamp = datetime.fromtimestamp(filename.stat().st_mtime).strftime("%Y%m%d")
  15. new_name = new_name[:-4] + "_" + new_timestamp + new_name[-4:]
  16. new_filename = target.joinpath(new_name)
  17. print(filename.name + " => " + new_filename.name)
  18. filename.rename(new_filename)
  19. break
  20. if __name__ == "__main__":
  21. main()