#๐Ÿ”’ How can I access files outside the directory of a py file

10 messages ยท Page 1 of 1 (latest)

faint hatch
#

I keep getting this error from this line

back_card = pygame.image.load("assets/card_graphic/go_fish/back_card.png")

back_card = pygame.image.load("assets/card_graphic/go_fish/back_card.png")

       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

FileNotFoundError: No file 'assets/card_graphic/go_fish/back_card.png' found in working directory 'PythonGoFish/.project/source'.

source and assets are two folders in .project

jade riverBOT
#

@faint hatch

Python help channel opened

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.

gentle hatch
#

how are you running the code?

#

if the directory you are running the code from is in the source folder, it will look for assets inside the source folder

faint hatch
#

For some reason whenever I try downloaded my project from github I would have to move all of the files outside of source inorder for them to work so I though directly stating the paths would fix that

#

So there is no possible way of using folders outside the directory of the py file

silver tiger
#

It is very possible, depending on the methodology, if simply by trying to input the path no because it is behind a directory. But if you go by first getting the full path of your working directory then from that get the asset, yes, as such

import os

parent_path = os.path.dirname(os.getcwd())

This in your case would result in PythonGoFish/.project/ as the parent_path which you can simple add your /assets/... after

torpid bloom
#

you can give it full path

jade riverBOT
#
Python help channel closed for inactivity

This help channel has been closed. 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.