Search code examples
pythonvariablesfor-loopprogram-entry-point

How to pass a new variable to python script using 'for' loop


I am trying to write a python script that will traverse through a list and then pass a new variable to an other python script. Here is my code:

Script: ENCViewer.py

 # import necessary modules
 import os

 # list of all ENCS
 root = os.listdir('/home/cassandra/desktop/file_formats/ENC_ROOT')
 root.sort()
 root = root[2:]

 for ENC in root:
      # pass new instance of variable 'ENC' to ENCReader.py
     import ENCReader.py

Script: ENCReader.py

from __main__ import *
print ENC
.... # remaining script

Currently, when executing the first code, ENCViewer.py, it will only execute once then exit. How can I pass new instance variables of 'ENC' to ENCReader.py so that it executes throughout the entire 'for' loop seen in the first snippet of code?

Thanks.


Solution

  • I don't know if what you are asking is possible, but I think that you miss understood the idea of creating modules and importing code. The "standard" way of achieving the same result is the following:

    ENCReader.py

    def printer(var):
        print(var)
        # your code..
    

    ENCViewer.py

     import os
     from ENCReader import printer
    
     # list of all ENCS
     root = os.listdir('/home/cassandra/desktop/file_formats/ENC_ROOT')
     root.sort()
     root = root[2:]
    
    for ENC in root:
        printer(ENC)