daydreamer
daydreamer

Reputation: 91969

Python adding config files to path

I have directory structure like following:

           analytics  
       /   /     \   \
    conf script  src  other  
    /  \  /  \   /  \   
   <setup>         <source>  

< setup >:  
       setup.yaml  
       sql.yaml  

< source  >:  
       src/  
           folder A/  
                    s1.py  
                    s2.py  
                    ...  
           folder B/
                    m1.py  
                    m2.py  
                    ...  

How can I inclue the setup files in of this structure to the source files in structure without hardcoding the paths

I tried sys.path.append('< path >') but when I try to open the file I see error

>>> import sys
>>> sys.path.append('/Users/user/Documents/work/dw/analytics/conf')
>>> f = open('setup.yaml', 'r')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'setup.yaml'
>>> f = open('setup.yaml', 'r')

Upvotes: 1

Views: 209

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798686

open() (and all the other file access functions) don't care about sys.path. It's only used when importing modules.

Create your own variable that contains the path that contains your files, and use it.

Upvotes: 4

Related Questions