No 2D array is created! Python3

Question:

I need to create a two dimensional array, but somehow my code is not working. Elements are added to the array itself (in a loop), but if you try to print after the loop, nothing happens.

 def sub_open(path_rar, path_corp):
    if not os.path.exists(path_corp):
       os.makedirs(path_corp)
    for root,dirs,files in os.walk(path_rar):
        for folder in dirs:
           print(folder)
           file = file_corp(path_corp, folder)
           alligns = allign_times('.//Extracted_Data//', folder, file)
           print(alligns) - здесь тоже ничего не выводит((((


def allign_times(path_rar, folder, sub_corpus):
   alligns = []
   for root,dirs,files in os.walk(path_rar + folder + '//'):
        for file in files:
           print(file) ############
           sub_file = open(path_rar + folder + '//' + file,  'r').read()
           times = re.findall('\d\d:\d\d:\d\d,\d\d\d --> \d\d:\d\d:\d\d,\d\d\d', sub_file)
           data_times, data_reverse = transform(times) ##
           vals = [i for i in sorted(list(data_times.values()))]
           vals1 = [i for i in sorted(list(data_times.keys()))]
           allphrases = piece_to_file(data_times, sub_file, times, vals, data_reverse, vals1)##
           #print(allphrases)   - тут находятся элементы и выводятся
           alligns.append(allphrases) - вроде как добавляем
           #print(alligns)    - тут печатает 

    #print(alligns) - выходим из цикла и после принта вообще ничего не выводит
    return alligns     

How to solve this problem? Where is the mistake? How do I make it display a normal two-dimensional array when I call a function inside the sub_open function?
I would be grateful for your answer!

Answer:

You must create the alligns array outside the function body, as it is now a local variable for allign_times . Declare it at the very beginning of the file (before the functions), and in each of them, in the first line, write global alligns .

Scroll to Top