#!/usr/bin/env python3 """ Find import files from the x3 import archive folder and stick them together for a single reimport. Useful when stock levels prevented multiple shipments from importing in. """ import re import pathlib import pprint THIS_DIRECTORY = pathlib.Path(__file__).parent X3_IMPORT_ARCHIVE = THIS_DIRECTORY / "x3_imports" / "archive" REIMPORT_DIRECTORY = THIS_DIRECTORY / "reimports" LIST_OF_UNSHIPPED_ORDERS = [ 'O0218424' ] def get_positions(string): pos = [] for i, chr in enumerate(string): if string[i] == '_': pos.append(i) return pos def main(): #run through all files in list and combine them into a single import with open(REIMPORT_DIRECTORY / 'ZSHIP867.dat', 'w',encoding="utf-8", newline="\n") as combined_import_file: files = get_files() for file in files: with file.open( "r", encoding="utf-8", newline="\n" ) as individual_import_file: for line in individual_import_file: combined_import_file.write(line) #search the archive directory for the files, write their contents to a single file def get_files(): file_list = [] for file in X3_IMPORT_ARCHIVE.iterdir(): if file.name[:9] == 'ZSHIP867_': underscores = get_positions(file.name) pos1 = underscores[1]+1 pos2 = underscores[2] if file.name[pos1:pos2] in LIST_OF_UNSHIPPED_ORDERS: file_list.append(file) return file_list if __name__ == "__main__": main()