Zac
Zac

Reputation: 323

open all files inside list of list in python at once

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.

Upvotes: 0

Views: 91

Answers (1)

Eden Gibson
Eden Gibson

Reputation: 156

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

Upvotes: 1

Related Questions