英文:
I don't understand why I'm getting this error: "FileNotFoundError: [Errno 2] No such file or directory: 'style/style.css'"
问题
我正在尝试将CSS文件加载到我的Streamlit项目中,但我不断收到上述错误。我保证,一切看起来都没问题。
我在与应用程序文件"brookfield.py"相同的目录中创建了一个名为"style"的文件夹。在这个style文件夹中,我使用Notepad创建了一个CSS文件,并命名为"style.css"。
我使用以下代码加载了"style.css"文件:
#---- 使用本地CSS ----
def local_css(file_name):
with open(file_name) as f:
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
local_css("style/style.css")
然而,我收到以下错误:
FileNotFoundError: [Errno 2] No such file or directory: 'style/style.css'
Traceback:
File "C:\Users\Ronmi\Anaconda3\lib\site-packages\streamlit\scriptrunner\script_runner.py", line 557, in _run_script
exec(code, module.__dict__)
File "brookfield.py", line 33, in <module>
local_css("style/style.css")
File "brookfield.py", line 30, in local_css
with open(file_name) as f:
英文:
I'm trying to load a CSS file into my Streamlit project, but I keep getting the above error. I promise, everything looks OK.
I created a folder named "style" in the same directory as the app file "brookfield.py". Inside this style folder, I created a CSS file with Notepad and named it "style.css".
I used the following code to load the style.css file:
#---- USE LOCAL CSS ----
def local_css(file_name):
with open(file_name) as f:
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
local_css("style/style.css")
Yet, I’m getting the following error:
FileNotFoundError: [Errno 2] No such file or directory: 'style/style.css'
Traceback:
File "C:\Users\Ronmi\Anaconda3\lib\site-packages\streamlit\scriptrunner\script_runner.py", line 557, in _run_script
exec(code, module.__dict__)
File "brookfield.py", line 33, in <module>
local_css("style/style.css")
File "brookfield.py", line 30, in local_css
with open(file_name) as f:
答案1
得分: 4
相对路径不是相对于源文件,而是当前工作目录(CWD),因此无法找到style.css。
首先验证CWD。您可以通过添加以下内容来执行此操作
import os
print("当前工作目录:", os.getcwd())
将其添加到brookefield.py文件中,然后运行Streamlit应用程序(它将打印到控制台)。
我相当确信style/style.css不会存在于CWD中。将它们移动到CWD位置,然后再试一次。
英文:
Relative pathing is not relative to the source file, but the current working directory (CWD), therefore unable to find style.css.
First verify the CWD. You can do this by adding
import os
print("Current working directory:", os.getcwd())
To your brookefield.py file and by running the Streamlit application (it'll get printed to the console).
I'm pretty sure style/style.css won't be present in the CWD. Move these over to the CWD location and try again.
答案2
得分: -4
发生的错误是"FileNotFoundError: [Errno 2] No such file or directory: 'style/style.css'",表明Python脚本无法找到位于"style"文件夹中的"style.css"文件。这可能是由于执行脚本时的当前工作目录引起的。
要解决此问题,您可以尝试以下步骤:
- 检查当前工作目录(CWD):
打印当前工作目录以确保它是包含"brookfield.py"和"style"文件夹的目录。
import os
print(os.getcwd())
- 指定完整文件路径:
不要依赖CWD,提供"style.css"文件的完整文件路径,以确保脚本无论CWD如何都能找到它。
import os
import streamlit as st
def local_css(file_name):
file_path = os.path.join(os.path.dirname(__file__), "style", file_name)
with open(file_path) as f:
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
local_css("style.css")
通过使用os.path.dirname(file),我们获取当前脚本的目录路径,然后将其与"style"文件夹和"style.css"文件名连接起来,创建完整的文件路径。这确保脚本将无论CWD如何都能找到"style.css"文件。
- 验证文件权限:
确保"style.css"文件具有必要的读取权限,并且文件夹结构正确。
英文:
The error you're encountering, "FileNotFoundError: [Errno 2] No such file or directory: 'style/style.css'", indicates that the file "style.css" in the "style" folder is not being found by the Python script. This is likely due to the current working directory when the script is executed.
To fix this issue, you can try the following steps:
- Check the Current Working Directory (CWD):
Print the current working directory to ensure it's the directory where your "brookfield.py" and "style" folder are located.
import os
print(os.getcwd())
- Specify the Full File Path:
Instead of relying on the CWD, provide the full file path to the "style.css" file to ensure the script can find it regardless of the CWD.
import os
import streamlit as st
def local_css(file_name):
file_path = os.path.join(os.path.dirname(__file__), "style", file_name)
with open(file_path) as f:
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
local_css("style.css")
By using os.path.dirname(file), we get the directory path of the current script, and then we join it with the "style" folder and the "style.css" file name to create the full file path. This ensures that the script will find the "style.css" file regardless of the CWD.
- Verify File Permissions:
Ensure that the "style.css" file has the necessary read permissions, and the folder structure is correct.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论