0

I am using Python's regex to extract paths from a file and then trying to append the paths into a single list. The paths are specified in different lines in the file (options) as follows:

EXE_INC = \
    -I$(LIB_SRC)/me/bMesh/lnInclude \
    -I$(LIB_SRC)/mTools/lnInclude \
    -I$(LIB_SRC)/dynamicM/lnInclude

The function that I have is:

def libinclude():
with open('options', 'r') as options:
    for lines in options:
        if 'LIB_SRC' in lines:
            result = []
            lib_src_path = re.search(r'\s*-I\$\(LIB_SRC\)(?P<lpath>\/.*)', lines.strip())
            lib_path = lib_src_path.group(1).split()
            result.append(lib_path[0])
            print result
return (result)

The output that I am getting is:

['/me/bMesh/lnInclude']
['/mTools/lnInclude']
['/dynamicM/lnInclude']

However, as you can see I get three different lists, each obtained for one of the lines in the file, options. Is there a way to get these three paths in one list so that the values are available outside the function.

1 Answer 1

2

For sure, you're creating a new list every time you find a match. So only create one list and just continue to append to it each time you find a match

def libinclude():
with open('options', 'r') as options:
    result = []
    for lines in options:
        if 'LIB_SRC' in lines:
            lib_src_path = re.search(r'\s*-I\$\(LIB_SRC\)(?P<lpath>\/.*)', lines.strip())
            lib_path = lib_src_path.group(1).split()
            result.append(lib_path[0])
            print result
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Greg. Indeed trivial!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.