Skip to content

Instantly share code, notes, and snippets.

@meilinger
Created February 24, 2017 21:09
Show Gist options
  • Save meilinger/5e2212e58d476de2734d1c16ba02092b to your computer and use it in GitHub Desktop.
Save meilinger/5e2212e58d476de2734d1c16ba02092b to your computer and use it in GitHub Desktop.
Python `open` files relative to executing script
# In order open a file relative to the executing script path (vs relative to the working directory of the python interpreter)
# use:
import os
path = 'a/relative/file/path/to/this/script/file.txt'
with open(os.path.join(os.path.dirname(__file__), path), 'r') as input_file:
content = input_file.read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment