[Solved] Python function,issue with input file [closed]


The format of the input file depends on the extension that you use, a .txt file will be a Tab Separated Values (tsv) file while a .csv file will be a Comma Separated Values (csv) file (please note that this is not a general convention, it is something that was decided by that colleague of yours that wrote the function, or maybe it’s a local convention).

Each line of the file is usually composed by three {tab,comma} separated values, i.e., frequency, real part and imaginary part of a complex value.

I said usually composed because the code silently discards all the
lines for which the element count is less than three.

There is something here and there that can be streamlined in the code,
but it’s inessential.
Rather, to answer your question re closing the file, change the first part
of the function to

def bbcalfunc(bbfile,nfreqlst):

    #define the delimiter
    if bbfile.find('.txt')>=0:
        delimiter="\t"
    elif bbfile.find('.csv')>=0:
        delimiter=","

    # slurp the file
    with file(bbfile,'r') as fid:
        fidlines=fid.readlines()

    ...

solved Python function,issue with input file [closed]