Get path from st.file_uploader

i have certain functions that take only path of a file.
is there a widget that i can use to browse through my system, select a file and this widget returns the path of that file?

st.file_uploader returns the file name but not the path.

Please help

Hey @Swaroop_Mg,

Are you looking to return the path of the file on your local machine, or the path of the file once itโ€™s uploaded via the st.file_uploader widget?

@Caroline, thank for writing back
im looking to return the path of the file on local machine.

Hi there, I went through the same situation too. I need to pass to a function the path of a specific folder but I donโ€™t actually need to upload any file at all.

Searching through the Streamlit blog it seems like a lot of users had the same issue, but the feature that allows to browse your local directories and choose a file/folder for retrieving the corresponding path has never been implemented

With โ€˜uploaded_file.nameโ€™ you can actually get the name of the file but not the relative path

I searched this before as well, the st.fie_uploader returns a fileIO object but not a traditional file. There is no path but only file name.

Hi @Swaroop_Mg

There are a couple of ways that you can display files or directories from which the app is running on. Here is an example of one way.

You can check out the GitHub repo of this at GitHub - sfc-gh-cnantasenamat/sandbox
Demo app at https://sandbox-app.streamlit.app/

Hope this helps!

Found a fix:

import tempfile
uploaded_file = st.file_uploader("File upload", type="pdf")
if uploaded_file:
        temp_dir = tempfile.mkdtemp()
        path = os.path.join(temp_dir, uploaded_file.name)
        with open(path, "wb") as f:
                f.write(uploaded_file.getvalue())

this will write the file that is uploaded to a temp folder and the path to the same temp folder is saved as path.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.