1

I'm using the following code to run multiple .py files at once. The files are all very specific and prone to change very often. So it's easier for me to individualise them so I can change them whenever I need to.

calculate=(
    'indicator1.py',
    'indicator2.py',
    'indicator3.py',
    'indicator4.py',
    'indicator5.py',
    'indicator6.py',
    'indicator7.py')

for file in calculate:
    exec(open(file,'rb').read())

The problem arises when I get an error. I have no way to immediately know which file has the problem.

How can I add some lines of code that return the specific file from calculate where the error was found? If possible, also in which line of that specific file.

Thank you so much!

TeresaR
  • 11
  • 2
  • Add a print - ```for file in calculate: print(file)``` . The full error message would help. Also why use exec to read the file? – Sean C Jun 21 '22 at 12:31
  • Print(file) works great! Thank you. It was much simpler than I thought it would be. I use exec because the .py files have a script that I need to run. I separate the scripts into individual files, so I can easily change them when in need, and I execute them in a Main file. Also, if I want to run only 3 or 4 indicators, I can comment the others. – TeresaR Jun 21 '22 at 13:31
  • the exec threw me a bit but seems valid if unusual- https://stackoverflow.com/questions/1186789/what-is-the-best-way-to-call-a-script-from-another-script also 'icecream' is a nice alternative to using print for debugging https://towardsdatascience.com/do-not-use-print-for-debugging-in-python-anymore-6767b6f1866d – Sean C Jun 21 '22 at 13:53
  • Thank you for the tip. I didn't know icecream. – TeresaR Jun 21 '22 at 15:17

0 Answers0