Skip to content
Advertisement

File doesnt exist error before user inputs file name

I am working with streamlit in python to produce a tool that takes a user’s input of a csv filename, and then carries out cleaning/tabulating of the data within the file.

I have encountered an issue where before the user has entered their filename, my streamlit site shows a “FileNotFoundError: [Errno 2] No such file or directory:”

This is expected because the user has not entered their filename yet – however once filename is entered the code runs smoothly. I am hoping to overcome this issue but as a relative newcomer to Python I am quite unsure how!

Please see code snippet below

JavaScript

The exact error I get before any user input has been taken is:

JavaScript

Thanks in advance to anyone who can offer a suggestion!

Advertisement

Answer

The general pattern for both Streamlit and Python in general is to test for the value existing:

JavaScript

When the Streamlit app runs before a user inputs something, the value of autocall_gbp_file is None. By writing if autocall_gbp_file:, you’re only running the pandas read_csv after someone has entered a value.

Separately, you’re better off developing this with st.file_uploader than using text_input, as the Streamlit app doesn’t necessarily have access to the user filesystem and same drive mapping as the machine you are developing on. By using st.file_uploader, you’re literally providing the actual file, not a reference to where it might be located.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement