FileNotFoundError: [Errno 2] No such file or directory. Yes, I'm using relative path

I have read a couple of the same topics on the forums and it boils down to relative path.

I’m deploying my app on streamlit cloud from my GitHub repository (https://github.com/dstark1993/nano-report). The error occurs when I’m trying to open all the .csv files in a folder (which is in the same directory as my app.py that runs!).

The error:

FileNotFoundError: [Errno 2] No such file or directory: 'Data\\Payment'

So a selection of payment is being made which sets the path to the payment folder and calls for a function to open the files and create a dataframe:

if selection == 'Payment':
    path = r'Data\Payment'
    df = open_df(path)
def open_df(path):
    files = [f for f in listdir(path) if isfile(join(path, f)) and f.endswith('.csv')]
    dataframes = list()
    for file in files:
        df = pd.read_csv(join(path, file))

Everything works smoothly if I run the app localy (i.e. streamlit run app.py)

You might want to do the following

from pathlib import Path

path = Path(__file__).parent / "Data/Payment"

Or something related. I used Path(__file__).parent / to specifically go back to my repos root

Found the solution at:
python - share.streamlit can’t find pkl file - Stack Overflow

1 Like

If it is really the same, take into account that Data/Payment is not the same as Data\Payment.

1 Like

As @Goyo has pointed out, there’s a difference between forward and backward slashes. I can get Streamlit to read file paths correctly with either one in my local environment, but it requires forward slashes in the cloud environment.

import streamlit as st
import pandas as pd


try:
    st.write('Trying with back slashes')
    st.dataframe(pd.read_csv(r'.\\files\\test.csv'))
except:
    st.write('It didn\'t work with back slashes.')


try:
    st.write('Trying with forward slashes')
    st.dataframe(pd.read_csv(r'files/test.csv'))
except:
    st.write('It didn\'t work with forward slashes.')

Local Result

image

Streamlit Cloud Result

image

1 Like

Try using the exact, or absolute, path. The terms absolute and relative also have their usual English meaning. A relative path shows where something is relative to some start point; an absolute path is a location starting from the top.

When you open a file with the name “filename.ext”; you are telling the open() function that your file is in the current working directory . This is called a relative path.

file = open('filename.ext') //relative path

In the above code, you are not giving the full path to a file to the open() function, just its name - a relative path. The error “FileNotFoundError: [Errno 2] No such file or directory” is telling you that there is no file of that name in the working directory. So, try using the exact, or absolute path.

file = open(r'C:\path\to\your\filename.ext') //absolute path

In the above code, all of the information needed to locate the file is contained in the path string - absolute path.

It’s a common misconception that relative paths are relative to the location of the python script, but this is untrue. Relative file paths are always relative to the current working directory, and the current working directory doesn’t have to be the location of your python script. . In order to make this work, the directory containing the python executable must be in the PATH, a so-called environment variable that contains directories that are automatically used for searching executables when you enter a command. In any case, if your Python script file and your data input file are not in the same directory, you always have to specify either a relative path between them or you have to use an absolute path for one of them.

1 Like

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