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
  • 3
    Just use a list or dict of files. No need for dynamically named variables. Commented Feb 10, 2018 at 0: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? Commented Feb 10, 2018 at 0:42
  • 2
    Why are you globbing for filenames when you already know the filename? Commented Feb 10, 2018 at 0:47

3 Answers 3

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}
Sign up to request clarification or add additional context in comments.

1 Comment

Watch out if the second argument is mutable, though: d = dict.fromkeys(keys, []); d['Test'].append('foo'); print(d)
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.

1 Comment

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)
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']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.