0

I have a python script that uses json to store data. In the data, there are also file names, so I was wondering if I could import a file using a variable. Example~

file = "apps/messanger"
import file as msg

If this isn't possible, I would have confirmed my hypothesis and just import all of my files separately. But, if it is possible, I would like to know how just because it would make my life easier.

Thanks for any help! -Jester

  • Does this answer your question? [How do I import other Python files?](https://stackoverflow.com/questions/2349991/how-do-i-import-other-python-files) (It covers both "standard" importing and also dynamically using strings/variables) – CrazyChucky Aug 31 '22 at 21:20

1 Answers1

-1

I'm not too good with python but when you handle files you normally use

file = open("path to file", 'r or w') # r for read, w for write

file.close() # when you are done with the file you must close it

If you are going to name it msg, then change the variable from file to msg, like

msg = open("apps/messenger", 'r')

msg.close() # when finished with the file
nbslay
  • 1
  • 4