def pack(file_array, args):
    'Packs files or folders given into the first argument: a target file name or a directory (archive will be named the same as directory).\n    \n    If archive name exists, appends number and tries again.'
    output_target = file_array[0]
    input_set = file_array.copy()
    if os.path.isdir(output_target):
        output_target = os.path.basename(os.path.realpath(output_target.rstrip('\\/')))
        if (len(output_target) == 0):
            print('Error: Archive name invalid.', file=sys.stderr)
            return
    elif ((len(output_target) > 4) and (output_target[(- 4):].upper() == '.DAT')):
        input_set = file_array[1:]
    else:
        print('Error: Unknown file(s). Please provide a .DAT file or existing folder to pack it.', file=sys.stderr)
        return
    input_files = []
    for f in input_set:
        for (dirpath, dirnames, filenames) in os.walk(f):
            for dirname in dirnames:
                if (dirname.startswith('.') or dirname.startswith('__')):
                    dirnames.remove(dirname)
            input_files += [os.path.join(dirpath, name) for name in filenames if ((not name.startswith('.')) and (not name.startswith('__')))]
    try_count = 0
    alt = ''
    while os.path.exists('{}{}.dat'.format(output_target, alt)):
        try_count += 1
        alt = '-{}'.format(try_count)
        if (try_count >= 100):
            print('Error: Archive output file exists and no alternative.', file=sys.stderr)
            return
    output_target = '{}{}.dat'.format(output_target, alt)
    if (not args.quiet):
        print('Packing {} files into {}...'.format(len(input_files), output_target))
    file_tables = []
    for file_path in input_files:
        try:
            (name, ext) = os.path.splitext(os.path.basename(file_path).lower())
            try:
                name = name.encode('ascii')
                ext = ext.encode('ascii')
            except UnicodeError:
                print('Error: Input file names must be valid ASCII. {} skipped.'.format(file_path), file=sys.stderr)
                continue
            if (len(ext) != 4):
                print('Error: Input file names must have 3 character extensions. {} skipped.'.format(file_path), file=sys.stderr)
                continue
            ext = ext[1:]
            if ((len(name) < 1) or (len(name) > 8)):
                print('Error: Input file names must be <= 8 characters in length. {} skipped.'.format(file_path), file=sys.stderr)
                continue
            if (b'.' in name):
                print('Error: Input file names cannot have multiple extensions or additional dots. {} skipped.'.format(file_path), file=sys.stderr)
                continue
            this_name_obj = {'name': name, 'size': os.path.getsize(file_path), 'pos': None, 'full_name': file_path}
            this_ext_table = None
            for table in file_tables:
                if (table['name'] == ext):
                    this_ext_table = table
                    break
            if (this_ext_table is None):
                this_ext_table = {'name': ext, 'count': None, 'pos': None, 'files': [this_name_obj]}
                file_tables.append(this_ext_table)
            else:
                this_ext_table['files'].append(this_name_obj)
        except Exception as err:
            print(('Error: Uncaught exception locating file: ' + file_path), file=sys.stderr)
            print('{}'.format(err), file=sys.stderr)
            if (not args.quiet):
                traceback.print_exc()
            return
    try:
        pos = 8
        ft_count = len(file_tables)
        pos += (ft_count * 12)
        for ft in file_tables:
            fd_count = len(ft['files'])
            ft['count'] = fd_count
            ft['pos'] = pos
            pos += (fd_count * 16)
        for ft in file_tables:
            for fd in ft['files']:
                fd['pos'] = pos
                pos += fd['size']
        with open(output_target, 'wb') as f:
            pos = 0
            pos += write_into(f, '<II', 12345678, ft_count)
            for ft in file_tables:
                f.write(ft['name'].ljust(4, b'\x00'))
                pos += 4
                pos += write_into(f, '<II', ft['pos'], ft['count'])
            for ft in file_tables:
                for fd in ft['files']:
                    f.write(fd['name'].ljust(8, b'\x00'))
                    pos += 8
                    pos += write_into(f, '<II', fd['size'], fd['pos'])
            for ft in file_tables:
                for fd in ft['files']:
                    file_path = fd['full_name']
                    if (not args.quiet):
                        print(file_path)
                    try:
                        with open(file_path, 'rb') as fi:
                            f.write(fi.read(fd['size']))
                    except Exception as err:
                        print(((('Error: Uncaught exception writing file to archive: ' + output_target) + ' <- ') + file_path), file=sys.stderr)
                        print('{}'.format(err), file=sys.stderr)
                        if (not args.quiet):
                            traceback.print_exc()
                        return
    except Exception as err2:
        print(('Error: Uncaught exception writing archive: ' + output_target), file=sys.stderr)
        print('{}'.format(err2), file=sys.stderr)
        if (not args.quiet):
            traceback.print_exc()
Packs files or folders given into the first argument: a target file name or a directory (archive will be named the same as directory).

If archive name exists, appends number and tries again.
def pack(file_array, args):
    'Packs files or folders given into the first argument: a target file name or a directory (archive will be named the same as directory).\n    \n    If archive name exists, appends number and tries again.'
    output_target = file_array[0]
    input_set = file_array.copy()
    if os.path.isdir(output_target):
        output_target = os.path.basename(os.path.realpath(output_target.rstrip('\\/')))
        if (len(output_target) == 0):
            print('Error: Archive name invalid.', file=sys.stderr)
            return
    elif ((len(output_target) > 4) and (output_target[(- 4):].upper() == '.DAT')):
        input_set = file_array[1:]
    else:
        print('Error: Unknown file(s). Please provide a .DAT file or existing folder to pack it.', file=sys.stderr)
        return
    input_files = []
    for f in input_set:
        for (dirpath, dirnames, filenames) in os.walk(f):
            for dirname in dirnames:
                if (dirname.startswith('.') or dirname.startswith('__')):
                    dirnames.remove(dirname)
            input_files += [os.path.join(dirpath, name) for name in filenames if ((not name.startswith('.')) and (not name.startswith('__')))]
    try_count = 0
    alt = 
    while os.path.exists('{}{}.dat'.format(output_target, alt)):
        try_count += 1
        alt = '-{}'.format(try_count)
        if (try_count >= 100):
            print('Error: Archive output file exists and no alternative.', file=sys.stderr)
            return
    output_target = '{}{}.dat'.format(output_target, alt)
    if (not args.quiet):
        print('Packing {} files into {}...'.format(len(input_files), output_target))
    file_tables = []
    for file_path in input_files:
        try:
            (name, ext) = os.path.splitext(os.path.basename(file_path).lower())
            try:
                name = name.encode('ascii')
                ext = ext.encode('ascii')
            except UnicodeError:
                print('Error: Input file names must be valid ASCII. {} skipped.'.format(file_path), file=sys.stderr)
                continue
            if (len(ext) != 4):
                print('Error: Input file names must have 3 character extensions. {} skipped.'.format(file_path), file=sys.stderr)
                continue
            ext = ext[1:]
            if ((len(name) < 1) or (len(name) > 8)):
                print('Error: Input file names must be <= 8 characters in length. {} skipped.'.format(file_path), file=sys.stderr)
                continue
            if (b'.' in name):
                print('Error: Input file names cannot have multiple extensions or additional dots. {} skipped.'.format(file_path), file=sys.stderr)
                continue
            this_name_obj = {'name': name, 'size': os.path.getsize(file_path), 'pos': None, 'full_name': file_path}
            this_ext_table = None
            for table in file_tables:
                if (table['name'] == ext):
                    this_ext_table = table
                    break
            if (this_ext_table is None):
                this_ext_table = {'name': ext, 'count': None, 'pos': None, 'files': [this_name_obj]}
                file_tables.append(this_ext_table)
            else:
                this_ext_table['files'].append(this_name_obj)
        except Exception as err:
            print(('Error: Uncaught exception locating file: ' + file_path), file=sys.stderr)
            print('{}'.format(err), file=sys.stderr)
            if (not args.quiet):
                traceback.print_exc()
            return
    try:
        pos = 8
        ft_count = len(file_tables)
        pos += (ft_count * 12)
        for ft in file_tables:
            fd_count = len(ft['files'])
            ft['count'] = fd_count
            ft['pos'] = pos
            pos += (fd_count * 16)
        for ft in file_tables:
            for fd in ft['files']:
                fd['pos'] = pos
                pos += fd['size']
        with open(output_target, 'wb') as f:
            pos = 0
            pos += write_into(f, '<II', 12345678, ft_count)
            for ft in file_tables:
                f.write(ft['name'].ljust(4, b'\x00'))
                pos += 4
                pos += write_into(f, '<II', ft['pos'], ft['count'])
            for ft in file_tables:
                for fd in ft['files']:
                    f.write(fd['name'].ljust(8, b'\x00'))
                    pos += 8
                    pos += write_into(f, '<II', fd['size'], fd['pos'])
            for ft in file_tables:
                for fd in ft['files']:
                    file_path = fd['full_name']
                    if (not args.quiet):
                        print(file_path)
                    try:
                        with open(file_path, 'rb') as fi:
                            f.write(fi.read(fd['size']))
                    except Exception as err:
                        print(((('Error: Uncaught exception writing file to archive: ' + output_target) + ' <- ') + file_path), file=sys.stderr)
                        print('{}'.format(err), file=sys.stderr)
                        if (not args.quiet):
                            traceback.print_exc()
                        return
    except Exception as err2:
        print(('Error: Uncaught exception writing archive: ' + output_target), file=sys.stderr)
        print('{}'.format(err2), file=sys.stderr)
        if (not args.quiet):
            traceback.print_exc()