Esempio n. 1
0
def open_awk(filelist:list, pos_args:list, delim):
    ''' Call awk command and return an opened pipe for read the output of awk, eg:
        for line in open_awk([file1, file2], [2,3,4], ',').stdout:
            print(line)   
    '''
    if isinstance(delim, (bytes, bytearray)):
        delim = str(delim, 'utf-8')

    if not filelist:
        raise ValueError('filelist')

    if not pos_args:
        raise ValueError('pos_args')

    if len(delim) != 1:
        raise ValueError('delim must be a length 1 char')

    for fn in filelist:
        if not exists(fn):
            raise FileNotFoundError(fn)

    for i in pos_args:
        if not isinstance(i, (int, str)):
            raise TypeError(pos_args)
        if isinstance(i, str) and not i.isdigit():
            raise TypeError(pos_args)

    cmd = AWK_CMD.safe_substitute(delim=delim,
                                  vargs=','.join(map(lambda x:'$' + str(x), pos_args)),
                                  files=' '.join(filelist))
    if __debug__:
        print(cmd)

    return OSCommand.popen(cmd)
Esempio n. 2
0
def file_count(textfile:str) -> tuple:
    ''' Count lines, words, bytes in text file '''
    err, ret = OSCommand.call('wc ' + textfile, False)
    if err:
        raise RuntimeError(os.strerror(err))
    if ret:
        nline, nword, nbyte, _ = ret.split()
    return int(nline), int(nword), int(nbyte)