import os from pathlib import Path def ift_backup(base_dir=None): if base_dir is None: base_dir = '/home/robert/projekte/python/BMW_Wien/IFT' source_path = Path(base_dir) / 'prod' target_path = base_dir + '/archive' for source_file in source_path.glob('*.txt'): # print(source_file) file_temp = source_file.name[:-4] if file_temp.count('_') == 2: prefix, filetype, timestamp = file_temp.split('_') year = timestamp[:4] month = timestamp[4:6] target = f"{target_path}/{filetype}/{year}/{year}-{month}/{source_file.name}" os.makedirs(Path(target).parent, exist_ok=True) print(target) # source_file.rename(target) if __name__ == '__main__': ift_backup()