Use a list generated in another Python file

0

I have a Python file with a function where I generate a list. This list will be used in other Python files. I'm doing it by importing the Python function that generates the list.

How do I load the list in the other files?

Python code that generates the list:

def elementID (my_list):

    input_file_path = "archivo.txt"
    my_list = []
    with open(input_file_path, "r") as in_file:
        for line in in_file:
            str = line.strip()
            my_list.append(str)
    
asked by Manuel Jurado 10.04.2018 в 10:52
source

1 answer

2

Code where the list is obtained:

#def elementID ():
#
#    input_file_path = "Elements_to_extract_forces.txt"
#    elements_list = []
#    with open(input_file_path, "r") as in_file:
#        for line in in_file:
#            str = line.strip()
#            elements_list.append(str)
#        #print elements_list
#    return elements_list

def elementID ():
    input_file_path = "Elements_to_extract_forces.txt"
    with open(input_file_path, "r") as in_file:
        return [line.strip() for line in in_file]

Code for another Python file where we load the generated list before:

from Read_element_IDs import elementID
element_list = elementID()
print element_list

Print the list on the screen. It works.

    
answered by 10.04.2018 в 11:13