I created this script on top of your function so you can select files recursively, which means you can select any file (not folder) path with any depth from your given path as you wish.
def file_selector(folder_path='.', target="background"):
filenames = [f for f in os.listdir(folder_path) if
not f[0] == "."] # get file names from dir excluding hidden files
selected_filename = st.selectbox(f'Select a {target}', filenames)
abs_path = os.path.join(folder_path, selected_filename)
if os.path.isdir(abs_path):
return file_selector(abs_path, target)
return os.path.join(folder_path, selected_filename)
It works something like this. If you have a better solution to get files recursively from multiple levels from a directory, let me know.
@aar2you, @Adrien_Treuille, guys, I am seeing the FileNotFoundError when I am trying to provide a file path with text input. Could you help me understand why and what I could about it? It worked well locally, but it doesn’t when I launch the app.
How do you expect it to work? It tries to look for that file on the host that it is running on, which is present on localhost (your computer) but not on the server. Files aren’t magic my friend
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.