0

I'm trying to use location_directory= os.path.dirname(os.getcwd()) for a folder location, but this function is returning a directory above, I will explain with results and what I ask for help on how to proceed. The framework what I´m using is dash . I´m trying to do this :

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

PATH = f"{location_diretory}/data/" 

When I try this PATH, return D:\name\data but the correct directory is D:\name\name\data.

I have 2 diretories in this project, i´m using pycharm for it. The current arquive .py, have the same name in the Diretory "name" and Diretory "name2".

bad_coder
  • 11,289
  • 20
  • 44
  • 72
  • Did you mean `os.path.join(os.getcwd(), 'data')`? – timgeb Feb 07 '22 at 16:34
  • You just need `"data"` to find a subdirectory of your current working directory with that name. See [What exactly is current working directory?](https://stackoverflow.com/questions/45591428/what-exactly-is-current-working-directory/66860904) and [Difference between `./` and `~/`](https://stackoverflow.com/a/55342466/874188) – tripleee Feb 07 '22 at 16:53
  • It's advisable to use [`Path.cwd()`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.cwd) currently. – bad_coder Feb 08 '22 at 10:09
  • You say in the comment below you are using PyCharm. The current working directory depends on how you are using your [run configurations](https://www.jetbrains.com/help/pycharm/run-debug-configuration.html) so that's a problem because in the question you don't explain exactly how you are executing you project (from where)? See the screenshot [in this answer](https://stackoverflow.com/a/66168966) where it says "working directory" in the screenshot is the field you should carefully configure (if you are executing directly from CMD/Shell that's your current working dir..) – bad_coder Feb 08 '22 at 10:19
  • I´m executing the code above in D:\name\name\arquive.py. I want to get data directory – Raphael Moral Piazera Feb 09 '22 at 18:32

1 Answers1

1

when you use location_directory= os.path.dirname(os.getcwd()) actually you search for not the current directory, but you search for parent directory (the directory thet containe the current directory)

use this:

location_directory= os.getcwd()
PATH = f"{location_diretory}/data/"
r3dnet
  • 36
  • 1
  • 6
  • I´m using Pycharm, i tried it, but this return other directory Arquive. I have same name of arquive in diferents directorys called cfg. I tried it but it´s not working for me – Raphael Moral Piazera Feb 07 '22 at 18:46