0

I have following problem:

I put in one variable the output from os.lostdir - this are .txt files. The number of files can vary each day. I want to assign every .txt file to a variable with the name of the .txt file.

Is this possible?

For example:

I have three .txt files a.txt, b.txt and c.txt. I want to assign the contents of a.txt to a variable a. However, the file names can change every day. Is there a way to do this in python?

The_C0der
  • 24
  • 6
  • 2
    What should happen if the file's name is something that can't be a Python identifier, for example `in.txt`, `or.txt`, `def.txt`, `this filename has spaces in it.txt`, `10_9_2018.txt` ...? – Kevin Oct 09 '18 at 15:54
  • 2
    Generally you should use a dict for this sort of thing. – Daniel Roseman Oct 09 '18 at 15:57
  • @Kevin good point. I guess OP could just use some character in the variable names to differentiate or something. –  Oct 09 '18 at 16:06
  • You should reconsider your approach for this problem altogether. Dynamic variable names are almost always a glaring design flaw. – juanpa.arrivillaga Oct 09 '18 at 17:35
  • Thank you guys - i was doing it with a dictionary. – The_C0der Oct 11 '18 at 15:03

0 Answers0