I need to create a two-dimensional array, but for some reason my code does not work. Elements are added to the array itself (in a loop), but if you try to output it through 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 to make so that a normal two-dimensional array is output when you call a function inside the sub_open function?
I would be grateful for the answer!

  • The allphrases array itself is quite voluminous, but this is not the reason .. - Alexander Naumov
  • Try to make a print cut or just its length. Where the print works, try to put it in the upper cycle - ivan K.
  • except you did not fall out ??? - ivan K.
  • Make sure you see stderr: import sys; print("stderr", file=sys.stderr); print("stdout") import sys; print("stderr", file=sys.stderr); print("stdout") import sys; print("stderr", file=sys.stderr); print("stdout") - jfs

1 answer 1

You must create an array of alligns outside the function body, as this is now a local variable for the function 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 .

  • run unnecessary global variables (as in this case). Note that alligns returns from allign_times . The name alligns in sub_open() not associated with the name alligns in allign_times() (you can use another name). Both names are local. - jfs