-1

I have thousands of text files containing very long block of text(around 200kb - 600kb each file). I want to store contents of each file in a separate variable and to make it easier to identify it I want to store them in a variable that is the same as the file name. Suppose there is a text in the file 'dog.txt' I want to store it's content within a variable 'dog'.

Would it be possible? And if not, I could use a dictionary but can it hold that large amount? Or are there any alternatives?

PS: Please stop downvoting :P I realised my mistake.

sP_
  • 1,738
  • 2
  • 15
  • 29

3 Answers3

1

The overhead of having a dictionary here is negligible. If you can handle a 600K string, you can also put it in a dictionary. So you should just use a dictionary from the file name to its contents:

files = {'dog.txt' : 'Lorem ipsum dolor sit amet', 'cat.txt' : 'meow'}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

You can solve this problem by reading the files into a dictionary, where the keys are the file names without the .txt extension and the values are the file contents. Something like this would work:

filenames = ['bird.txt', 'cat.txt', 'dog.txt']
data = {}

    for name in filenames:
        with open(name) as f:
            key = name.partition('.')[0]
            data[key] = f.read()

You can access the file contents by looking up the file name in the data dictionary:

contents_of_dog_file = data['dog']
snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
1

The best you can do is use a dictionary. But you can also use "exec()" to create variables on the spot, though a dictionary is the right tool for it. Supposing all your files don't start with a number nor contain any invalid characters for a variable name:

import os
fileNames = os.listdir(os.path.join(os.getcwd(), 'subfoldername'))
for fName in fileNames:
  f = open(fName, 'r')
  exec("{0} = '{1}'".join(fName.split('.')[0]), f.read())
  f.close()

print(dog) #prints out content of the file "dog.txt" 
Aiman Al-Eryani
  • 709
  • 4
  • 19
  • 1
    To be more explicit: *don't* do this. It's a [bad idea](http://stackoverflow.com/questions/1373164/how-do-i-do-variable-variables-in-python). Use a dictionary instead. – Chuck Mar 13 '16 at 08:42