V0.71.0 - file_uploader : I/O operation on closed file

I’ve been getting this error from file_uploader when I adjust a slider or do anything that reruns the app. I saw posts talking about using seek(0) on the file, but that didn’t seem to help. I upgraded to version 71, since it said that it was taking out the need to do a seek, but I’m still running into issues. Posted the error I’m seeing, and my code, any ideas would be great.

ValueError: I/O operation on closed file.


File "/Users/x/miniconda3/envs/streamlit/lib/python3.8/site-packages/streamlit/script_runner.py", line 332, in _run_script
    exec(code, module.__dict__)File "/Users/x/x/x/x/x/My_Streamlit.py", line 349, in <module>
    main()File "/Users/x/x/x/x/x/x/My_Streamlit.py", line 22, in main
    uploaded_file = st.file_uploader("Select the impact data file you would like to analyze.", type='.csv')File "/Users/x/miniconda3/envs/streamlit/lib/python3.8/site-packages/streamlit/elements/file_uploader.py", line 122, in file_uploader
    if file.tell() > 0:

This the code from that I’m using:

uploaded_file = st.file_uploader("Select the impact data file you would like to analyze.", type='.csv')

    if uploaded_file:        
        # Read in the file to a dataframe and find out what row the summary and raw data sections split at
        splitRow, dfFile, test_name = initial_read_data(uploaded_file)

def initial_read_data(aFile):
    test_name = aFile.name
    dfRead = pd.read_csv(aFile, skiprows=5, skipfooter=4, engine='python')
    foundRow = dfRead['Sample Name'].values.tolist().index("RAW DATA")

    return foundRow, dfRead, test_name

Hi @pdxnick, thanks for reporting this! I’ve been taking a look and have been able to reproduce very intermittently with your code. I’m still trying to get this to reproduce consistently. Do you get this error on each rerun or intermittently? Also, could you share the file you are using or alternatively what the size of your file is?

We’ll look into adding better error handling but I would love to get to the root cause of this issue. We’ve also reopened this GH issue to track the work

Hi @karriebear, sorry for the late response! This does happen intermittently for me too, sometimes it’s when I choose a radio button, and sometimes it happens when I adjust a slider that I have. After the error occurs if I re-upload the file it doesn’t seem to happen again for that file.

The files I’m using are typically around 1-3MB and might be 10-100k rows. I have a file I can share with you, but I don’t see how to attach something to the post? Can I send it to you or what’s the best way?


Sorry for the late response but glad to hear this is intermittent for you as well! I think I have found the cause and we are looking at addressing this.

Though I am able to reproduce with my data, I would always appreciate any additional data to run our tests against. If you could send over your data to karrie@streamlit.io, I’d really appreciate it and make sure to test with your dataset as well!

Doing some more testing I found that I will always get the error (ValueError: I/O operation on closed file.) the first time that I load a file after launching the app and then interact with a widget.

If I refresh my browser and then upload the same file over again, then it will work for that widget. I can then upload another file and most of the time the new file will work too if I adjust the same widget, but if I adjust a different widget (slider, radio) it might give the IO error, this is where the intermittent part comes in that I can’t figure out.

Once each widget has been used and caused the error one time, then I seem to be able to re-upload any file, new or old, and do anything without issues. When I terminate streamlit and relaunch the program that starts the sequence over.