#π FileNotFoundError Docker Container
55 messages Β· Page 1 of 1 (latest)
@upper stratus
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Alive
your process' "current working directory" isn't what you think it is
I spend a lot of time on the Python discord, answering questions from newbies, and a common question is βMy program is getting FileNotFoundError but I can see the file is RIGHT THERE!β Whatβs going on is: their script is trying to open a file that is in the same directory as the script theyβre using a relative file name like open("my-file.txt...
So?
so that's why it's not working
I saw the post, but am confused on what's going on here
either change the current working directory, or change the path
Can ya explain?
I am a little bit confused on this
oh, wait no
i read the post
let me see
Could you show your dockerfile? It usually marks what is work directory and how your file is run. This will show us what cwd is.
Also, you can use __file__ to get the path of the current python file and then make paths relative to it.
Like using pathlib your file would be
pathlib.Path(__file__).resolve().parent / "blog/First-Blog.md"
(.parent because we want to get the directory where the py file is in)
im curious abiut the overall setup here..
as when I do like a mysql db involved in a script, i spin up a mysql conatiner with a shared volume
this?
{
"name": "Python 3",
"image": "mcr.microsoft.com/devcontainers/python:1-3.12-bullseye",
"features": {
"ghcr.io/warrenbuckley/codespace-features/sqlite:1": {}
},
"workspaceFolder": "/workspaces/portfolio-web"
}
it's sqlite
i see that now..
You said docker container but this is not the docker format 
I don't see any steps of creating it - copying source, installing dependencies, etc +where is the entrypoint?
he might have some kind of dynamic parse for creating a docker compose yaml, not help seeing it in this format if so
It's the one you get useing vscode
this was the easy way apparently
Is the os.getcwd printing the correct directory for you?
You could also try
./blob/First-Blog.md for the file path
What I would really recommend is doing a os.listdir on whereever you keep the file and see if it got copied
Something like
os.listdir("./blog")
I did, and it printed out correctly:
['.obsidian', 'First-Blog.md']
Have you tried adding a "./" to your filepath, assuming that is where blog comes from
Can ya explain
Doesn't work either
It should be
./blob/First-Blog.md
import os
def print_dir_structure(path='.'):
for root, dirs, files in os.walk(path):
level = root.replace(path, '').count(os.sep)
indent = ' ' * 4 * level
print(f"{indent}{os.path.basename(root)}/")
subindent = ' ' * 4 * (level + 1)
for file in files:
print(f"{subindent}{file}")
print_dir_structure()
this a nice snipet to get the dir structure for your repo, if your doing them
it may help in this case
What should i put in the path?
Doesn't work
This is super weird
im confused, did you mean to ping me cause i dont see the output fromnthe func i gave you.. which you should runnat the start of the script to get the output.
I ran it with file . It did not give any output
It worked!
I just had to change the path to: /workspaces/portfolio-web/blog
Can somone explain the why of it now
if your loik8ng in the folder blog, then thats how you map to it.
in all the screenshots you sent you were nit inclyding blog as a folder.
phone sheshh
it's more of like: ah, it works
who knows why, but it works
Maybe because i was using relative paths
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.