2

I have a few hundred .txt files which I need to load and rather than type out variables for each of these I'd like way an automated process which will create a variable for each .txt file e.g.

Test.txt, Test1.txt and Test2.txt give variables Test, Test1, Test2

I will then subsequently load each .txt file to the corresponding variable e.g.

Test = glob.glob('Test.txt')
Test1 = glob.glob('Test1.txt')
Test2 = glob.glob('Test2.txt')

I can create a list of all the file names and strip of the '.txt' motif e.g. ['Test', 'Test1', 'Test2'] but I'm unsure how (or even if its possible) to convert items in this list to variables.

I've tried reading around and seen that global() or exec() may be useful but I've never used them and seen plenty of people warning from using these so keen for more experienced input.

thanks in advance

  • 3
    Just use a list or dict of files. No need for dynamically named variables. – internet_user Feb 10 '18 at 00:39
  • 2
    Suppose you had the capability you describe. What would you do then? You wouldn't be able to write any more code because you wouldn't know what variable names you had or what was in any of them. What are you trying to do here? – Paul Cornelius Feb 10 '18 at 00:42
  • 2
    Why are you globbing for filenames when you already know the filename? – kindall Feb 10 '18 at 00:47

3 Answers3

2

Why would you want to name your variables like this?

Instead, you should create a dictionary, using your list's elements as keys. Something like this:

keys = ['Test', 'Test1', 'Test2']
dict.fromkeys(keys, 0)

This code will give you this dictionary:

{'Test': 0, 'Test1': 0, 'Test2': 0}
Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
2

Don't create variable names from strings. In fact, it's instructive to understand that all* variables are actually dictionary keys themselves of some namespace.

In your case, you can set up a dictionary similar to the below:

d = {'Test'+str(i): glob.glob('Test'+str(i)+'.txt') for i in range(3)}

* But see comment by @juanpa.arrivillaga.

jpp
  • 159,742
  • 34
  • 281
  • 339
  • 1
    Nitpick: not all namespaces are implemented as actual dictionaries. Local namespaces are optimized to simple arrays, where thebytecode has already compiled the local names to positions in thsi array, allowing the `LOAD_FAST` and `STORE_FAST` ops to do their magic. With global variables, you have to actually check in a dictionary, consequently, this is why glbal name resolution is slower than local name resolution (and why dynamic modifications to `locals` aren't allowed in Python anymore) – juanpa.arrivillaga Feb 10 '18 at 00:54
0

Thanks for the responses, didn't realise dictionaries were so powerful, based on advice here and from a trained physicist friend:

files = {f.replace(".txt", ""): open(f, "r").read() for f in glob.glob('*.txt')}

I can then access and do as I please with the data by:

files['filename']