File_uploader() - I/O operation on closed file

my initial code fragment that gave the closed file error after a few reloads in a not really predictable manner:

    datastream = st.file_uploader('ascii tab data ')   
    extdata = []
    if datastream is not None:             
        datastream.seek(0)        
        stream = io.TextIOWrapper(datastream)
        extdata = textdata.read_textdata(stream)
        extdata = np.array(extdata['data'])
     

I found out that the problem is io.TextIOWrapper(). Even if I leave out the last 2 lines , the same error appears. My workaround is to buffer the bytestream first:

    datastream = st.file_uploader('ascii tab data ')   
    extdata = []
    if datastream is not None:             
        datastream.seek(0)        
        buf = datastream.read()
        buf = io.BytesIO(buf)
        stream = io.TextIOWrapper(buf)        
        extdata = textdata.read_textdata(stream)
        extdata = np.array(extdata['data'])

I have no clue why version 1 does not work. Anyone?

1 Like