11

Below folder structure of my application:

rootfolder
          /subfolder1/
          /subfolder2
          /subfolder3/test.py

my code inside of the subfolder3. But I want to write output of the code to subfolder1.

script_dir = os.path.dirname(__file__)

full_path = os.path.join(script_dir,'/subfolder1/')

I would like to know how can I do this wihout importing full path to directory.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Qaqa Leveo
  • 127
  • 1
  • 1
  • 6

2 Answers2

12

It sounds like you want something along the lines of

project_root = os.path.dirname(os.path.dirname(__file__))
output_path = os.path.join(project_root, 'subfolder1')

The project_root is set to the folder above your script's parent folder, which matches your description. The output folder then goes to subfolder1 under that.

I would also rephrase my import as

from os.path import dirname, join

That shortens your code to

project_root = dirname(dirname(__file__))
output_path = join(project_root, 'subfolder1')

I find this version to be easier to read.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • Print the intermediate results: `__file__`, `project_root` and `output_path`. Make sure they are correct. – Mad Physicist Dec 12 '17 at 23:00
  • all of them under my hand. Really can't understand what is wrong with this. All of them seems true – Qaqa Leveo Dec 12 '17 at 23:01
  • I mean show me the printouts. I want to be able to diagnose this. – Mad Physicist Dec 12 '17 at 23:01
  • `project_root = os.path.dirname(os.path.dirname(__file__))` only returns `NameError: name '__file__' is not defined`. Am I missing something here? – Jakob Mar 14 '23 at 20:03
  • @Jakob. What version of python are you using, and where is the module stored? – Mad Physicist Mar 14 '23 at 23:46
  • @MadPhysicist. Python version is 3.9.16 and if you with module mean the python path, it's in a Conda environment under `/Users/username/miniconda3/envs/conda_env/bin/python`. The project (and where I run the code from) is located under `/Users/username/Desktop/Projects/project_name` though. – Jakob Mar 15 '23 at 06:54
  • Hmm, okay. I tested it in a jupyter notebook (`.ipynb`) and that didn't work. It works in a `.py` file though. – Jakob Mar 15 '23 at 08:15
-2

The best way to get this done is to turn your project into a module. Python uses an __init__.py file to recognize this setup. So we can simply create an empty __init__.py file at the root directory. The structure would look like:

rootfolder
          /subfolder1/
          /subfolder2
          /subfolder3/test.py
          __init__.py

Once that is done, you can reference any subfolders like the following:

subfolder1/output.txt

Therefore, your script would look something like this:

f = open("subfolder1/output.txt", "w+")
f.write("works!")
f.close()
ZooM SiX
  • 75
  • 4
  • 1
    `__init__` makes it a package, not a module. You will then need an `__init__` in `subfolder3` to mark it as a sub-package. None of this, however, will affect where you put your output data. Also, use `with` to open files. – Mad Physicist Dec 13 '17 at 21:32
  • You're right, my fault! Marking subfolder3 as a sub-package isn't needed for this though. – ZooM SiX Dec 13 '17 at 23:10
  • The reference won't magically resolve itself unless you run from a very specific directory. That is why OP is using `__file__` and why having an `__init__` or not having it is totally irrelevant. – Mad Physicist Dec 13 '17 at 23:26