How To Get A Full Path Of A Sibling Folder In Python

  • Use only os.path.join()
  • Why? Different computers represent paths in different ways.
  • E.g. Windows uses \ as a separator, while Unix (Mac and Linux) uses /
  • In other words, when you see os.path.join() it means concatenation of a file-path
""" for a script in ./src to get the full path of the ../assets folder """
assets_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'assets'))
  1. os.path.dirname(__file__) returns the abs path of its parent folder (./src)
  2. os.path.join() concatenates the previous, with step-back control statement, and target folder
  3. os.path.abspath() normalizes the returned path for cross-platform compatibility
  • . do nothing
  • .. step-back
  • for os.path.abspath(path):

4. SOURCES

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pavol Kutaj

Pavol Kutaj

Infrastructure Support Engineer/Technical Writer (Snowplow Analytics) with a passion for Python/writing documentation. More about me: https://pavol.kutaj.com