0

I am using a code below to open file from list of list one by one

name = [['tina', 'vans', 'john', 'sam', 'victory'],['nanny', 'pink', 'sidewalk', 'paper', 'team'],['jimmy', 'rob', 'stack', 'layla', 'london']]

for i in name:
    for k in i:
        a = open(f'{k}.txt')

would like to know if it is possible to one set of list files at the same time like for ['tina', 'vans', 'john', 'sam', 'victory']. want to open this file then next list files.

msanford
  • 11,803
  • 11
  • 66
  • 93
Zac
  • 323
  • 7
  • 14
  • may be a duplicate of https://stackoverflow.com/questions/29550290/how-to-open-a-list-of-files-in-python – Tom Chen Jul 23 '21 at 16:07
  • Check https://stackoverflow.com/questions/4617034/how-can-i-open-multiple-files-using-with-open-in-python –  Jul 23 '21 at 16:07
  • The code already does that, doesn't it? The inner `for` opens the list of files then returns back to the outer `for` which will move to the next list. – tdelaney Jul 23 '21 at 16:08
  • no i want to last loop to open all files at the same time like: a = open('tina.txt'), b = open('vans.txt'), c = open('john.txt'), d = open('sam.txt'),e = open('victory.txt') all at the same time – Zac Jul 23 '21 at 16:11

1 Answers1

1

From what I know, you cannot open multiple files in the same variable at once.

It would be best to use the file to do what you would want to do, e.g storing the text on the file in a list, then going to the next one and doing the same. It's quite hard to give you a workaround if you don't say what you mean to do with the files

Here is what I would do:

name = [['tina', 'vans', 'john', 'sam', 'victory'],['nanny', 'pink', 'sidewalk', 'paper', 'team'],['jimmy', 'rob', 'stack', 'layla', 'london']]

a = open(f'{name[0][0]}.txt') #for one time use

def get_name(index1, index2): #for use wherever
    a = open(f'{name[index1][index1]}.txt')
    return a

currentName = get_name(0,0) #returns tina.txt file
Eden Gibson
  • 126
  • 1
  • 11