Streamlit.share.io fails to find local file - FileNotFoundError: [Errno 2] No such file or directory. -- Works in local mode

Hello all,
when deploying my app on streamlit.share.io, i’m having a classic FileNotFoundError: [Errno 2] No such file or directory.

I am sure of the path (tested it directly from the log file generated). This is strange since, when I’m using the app in standalone mode, it works perfectly as I want. I’ve tried multiple workaround found here on the forum, but for the life of me, I can’t get it to work. I join in attachement the screenshot of what I get. The app itself is running, but i get the error when I copy/past the path of the folder where my file is contained (without the filename, but I tried also with the file name, still fails), it get the error message in the picture. I’ll also join my code. It is part of a multipage app so no worries about the def app() stuff, it’s getting called somewhere else and there is no issue with that. The file is a .log file, but it’s not the cause of the problem either.

At some point, I thought it was the \ vs \ problem with the path, but it appears it is not (maybe yes, but I don’t think so, since I’ve tried a direct raw string or normal string and it works in standalone mode).

Does anyone have any idea?
Edit: the error message is giving a slightly different function name, because it’s from some troubleshooting, but the result is absolutely the same with what I copied here.

import plotly.graph_objects as go
import re
import os
import streamlit as st


###########################################
###########################################
###           Start & styling           ###
###########################################
###########################################

def app():
    
    
    oc_id = 'some text '
    install_oc = 'some text too'

    ###########################################
    ###########################################
    ###           File selector             ###
    ###########################################
    ###########################################

    def file_selector(folder_path):

         filenames = [os.path.abspath(os.path.join(folder_path, _)) for _ in os.listdir(folder_path) if _.endswith(".log")]
        selected_filename = st.selectbox('Select a file', filenames)
         st.write('You selected `%s`' % selected_filename)
        return os.path.join(selected_filename)

    file_input = st.text_input("Copy your file path here", placeholder="C:/")

    if len(file_input)==0 :
        st.warning("No file selected!")
    else:
        file = file_selector(str(file_input))

You cannot access files on your local computer in this way if you run the streamlit app on streamlit cloud. This does not work and is a conceptual misunderstanding. Think about it, where does this python app run, it runs on the server and there is no C: drive.

Yes, it makes sense indeed. Then how could I do it? The file_uploader widget does not work for this kind of files.

Why not? What kind of files?

my file is a “.log” file. Maybe I’m not doing it correctly, but I cannot make it work for this extension

Sure this should work. Unless your log file is very large in the hundreds of megabytes or beyond.

Hundreds, no. But usually around 60mb yes.

Should be no problem at all.

ok but then how do you read such a file? Because a “with open(…) as f:” classic way, I’m struggling a lot. I have other pages where I use the file_uploader widget without problems for a csv file, but for my log file, I’m stuck.

What is the difference? Where is the problem?

always had this usual error : TypeError: expected str, bytes or os.PathLike object, not _io.BytesIO

I can’t seem to open it as a str or bytes file, like i can for a csv

Can you provide a code example, that has not worked for you?

Sure, thanks a lot for all the help! :slight_smile:
What’s currently uncommented (with the open()…) does not work and returns an error “TypeError: expected str, bytes or os.PathLike object, not UploadedFile”.

I also tried the commented line above instead (with, of course, commenting the open() )) but then my line containing 'if string_to_search in line:" is messed up with types as well. It’s probably simpler than I think, I’m probably noze-dived in it for too long today.

ifile = st.file_uploader("Upload your log file here!", type=".log", accept_multiple_files=False)
        line_number = 0
        list_of_results = []
        ifile=ifile
        if ifile is not None:
            #f = ifile.readlines()
            with open(ifile, "rb") as f:
            # Read all lines in the file one by one
                for line in f:
                    # For each line, check if line contains the string
                    line_number += 1
                    if string_to_search in line:
                        # If yes, then add the line number & line as a tuple in the list
                        if octype == "actual":
                            line = line.replace(line[24:79], '')  # to remove the timestamp of file creation (not the event timestamp)
                            list_of_results.append(line.replace(oc_id, ''))
                        else:
                            line = line.replace(line[0:120], "")
                            list_of_results.append(line.replace(install_oc, " "))
        # Return list of tuples containing line numbers and lines where string is found
        return list_of_results

This is one possible solution:

uploaded_file = st.file_uploader("Upload your log file here!", type=".log", accept_multiple_files=False)
if uploaded_file is not None:
    lines = uploaded_file.readlines()
    for line in lines:
        print(line.decode("utf-8").strip())
1 Like

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