I want to get the absolute path of the brother directory in the directory where the executable is located.

Asked 2 years ago, Updated 2 years ago, 18 views

If you are running a script in the folder a, I would like to get the absolute path of c.py in the folder b in the same hierarchy as a in that script.

For example, a and b are in the same folder named d.

python

2022-09-30 11:27

2 Answers

From Comments

It's solved.We actually used os.path.dirname to specify the upper hierarchy.


2022-09-30 11:27

_init__.py must be placed in order to import anything below the b folder.
_init__.py should be empty.
You can specify where this file is located in import.

Folder Configuration

a/
├ ├ hoge.py
└ b/
  ├ __init__.py
  └ └ c.py

hoge.py

 from b.import something

something()

c.py

def something():
    print('test')

By the way, the import method can also be import b.c.
However, if you import it that way, it must be b.c.something() when calling the method.

Class definitions can also be invoked.

hoge.py

 from b.import something, TestClass

something()

test_class = TestClass()
test_class.something()

c.py

def something():
    print('test')


class TestClass():
    def__init__(self):
        pass

    def something (self):
        print('class test')

Check the documentation for details.
http://docs.python.jp/2/tutorial/modules.html#packages


2022-09-30 11:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.