0

I want to run a function that will create a file with the name of a variable and write in it the content of that variable.

list_A = 'one two three'.split()

def write_list(list_to_write): 
    flux = open('%s.list' %list_to_write, 'w')
    flux.write('\n'.join(list_to_write))
    flux.close()

write_list (list_A)

This creates a file called ['one', 'two', 'three'].list

I would like it to create a file called liste_A.list instead.

PS: I found many similar posts, but none that seemed to address my issue.

Jonath P
  • 519
  • 5
  • 16

1 Answers1

0

It's possible in other languages like tcl for example, but not in Python.

If you really need to manage lists and their names, you can use a dictionary for this, for example:

lists = {}
lists['list_A'] = 'one two three'.split()

...

write_list('list_A')

And then write_list would use lists[list_to_write] to access the list

Israel Unterman
  • 13,158
  • 4
  • 28
  • 35