Python Forum
Call a python file with anothr python file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Call a python file with anothr python file
#1
I have this code that pings a PLC to se if it is online if it is on it will start the PLC file that
corresponds to the IP, it works but when the PLC fiel ends it closes all
instead of going back to the code that validates the IP

whats wront with the code?

import subprocess
import sys
import os

hostname = '172.21.12.10'
response = os.system("ping " + hostname)


if response == 0:
    print (hostname, 'PLC EN LINEA, INICIANDO REVISION')
    exec(open("SE1_F1.py").read())
  
else:
    print ('SE1_F1 FUERA DE LINEA, EJECUTANDO SIGUIENTE SCRIPT')
    hostname = ' 192.168.100.5'
    response = os.system("ping " + hostname)
  
    if response == 0:
        print ('SE1_F2 EN LINEA, INICIANDO REVISION')
        exec(open("SE1_F2.py").read())
          
    else:
        print ('SE1_F2 FUERA DE LINEA, EJECUTANDO SIGUIENTE SCRIPT')
        hostname = '172.21.13.10'
        response = os.system("ping " + hostname)

        if response == 0:
            print('SE2_R1 EN LINEA, INCIANDO REVISION')
            exec(open("SE2_R1.py").read())
        else:
            print ('SE2_R1 FUERA DE LINEA, EJECUTANDO SIGUIENTE SCRIPT')
            hostname = '172.21.13.12'
            response = os.system("ping " + hostname)

            if response == 0:
                print('SE2_R2 EN LINEA, INCIANDO REVISION')
                exec(open("SE2_R2.py").read())
            else:
                print ('SE2_R2 FUERA DE LINEA, EJECUTANDO SIGUIENTE SCRIPT')
                hostname = '172.21.14.10'
                response = os.system("ping " + hostname)

                if response == 0:
                    print('SE3_M1 EN LINEA, INCIANDO REVISION')
                    exec(open("SE3_M1.py").read())
                else:
                    print ('SE3_M1 FUERA DE LINEA, EJECUTANDO SIGUIENTE SCRIPT')
                    hostname = '172.21.14.12'
                    response = os.system("ping " + hostname)

                    if response == 0:
                        print('SE3_M2 EN LINEA, INCIANDO REVISION')
                        exec(open("SE3_M2.py").read())
                    else:
                        print ('SE3_M2 FUERA DE LINEA, EJECUTANDO SIGUIENTE SCRIPT')
                        hostname = '172.21.11.10'
                        response = os.system("ping " + hostname)

                        if response == 0:
                            print('SE4_S1 EN LINEA, INCIANDO REVISION')
                            exec(open("SE4_S1.py").read())
                        else:
                            print ('SE4_S1 FUERA DE LINEA, EJECUTANDO SIGUIENTE SCRIPT')
                            hostname = '172.21.11.11'
                            response = os.system("ping " + hostname)

                            if response == 0:
                                print('SE4_S2 EN LINEA, INCIANDO REVISION')
                                exec(open("SE4_S2.py").read())
                            else:
                                print("NINGUN PLC EN LINEA")
the SE1_F2 is to loong to show it here
but i can show you the SE1_F2 (that is the one that is called if SE1_F2 is not responding)

from pylogix import PLC
import sys
sys.path.append('..')
import time
import pandas as pd

plc_data = pd.read_excel("Actuadores.xlsx", sheet_name='Result')
with PLC() as comm:
    comm.IPAddress = '172.21.12.12'
    comm.ProcessorSlot = 0
    ret = comm.GetPLCTime()
    print( "Fecha y Hora de Registro Revision Fallas", ret.Value)
    time.sleep(1)
    Estacion = "D2M_150"
    Estacion2 = "D2R_155"
#------------------------------------------------------------------------------------------ ESTACION D2M_150 -------------------------------------------------------------------------------#
    print("INICIO DE REVISION ESTACION :" , Estacion)
#ACTUADOR 1 
    ACTUADOR = comm.Read('D2M_150.Status.Act._1.WBypassed')
    ret = comm.GetPLCTime()
    Numero_Act_EnBypass = 0
    
    if ACTUADOR.Value == True:
        Sensor = "ACTUADOR 1 WORK SENSOR EN BY PASS"
        Numero_Act_EnBypass= (Numero_Act_EnBypass+1)
        temp_dict = {'PLC IP': comm.IPAddress, 'Date Time': ret.Value, 'Estacion': Estacion, 'Tag': ACTUADOR, 'Value': Sensor}
        plc_data = plc_data.append(temp_dict.copy(), ignore_index=True)
        plc_data.to_excel("Actuadores.xlsx", sheet_name='Result', index=False) 
        print(Sensor)

    else:
        Sensor = "ACTUADOR 1 WORK OK" 
        print(Sensor)
   


    ACTUADOR = comm.Read('D2M_150.Status.Act._1.HBypassed')
    ret = comm.GetPLCTime()
    if ACTUADOR.Value == True:
        Sensor = "ACTUADOR 1 HOME SENSOR EN BY PASS"
        Numero_Act_EnBypass= (Numero_Act_EnBypass+1)
        temp_dict = {'PLC IP': comm.IPAddress, 'Date Time': ret.Value, 'Estacion': Estacion, 'Tag': ACTUADOR, 'Value': Sensor}
        plc_data = plc_data.append(temp_dict.copy(), ignore_index=True)
        plc_data.to_excel("Actuadores.xlsx", sheet_name='Result', index=False) 
        print(Sensor)

    else:
        Sensor = "ACTUADOR 1 HOME OK"
        print(Sensor)
#ACTUADOR 2 
    ACTUADOR = comm.Read('D2M_150.Status.Act._2.WBypassed')
    ret = comm.GetPLCTime()
    
    if ACTUADOR.Value == True:
        Sensor = "ACTUADOR 2 WORK SENSOR EN BY PASS"
        Numero_Act_EnBypass= (Numero_Act_EnBypass+1)
        temp_dict = {'PLC IP': comm.IPAddress, 'Date Time': ret.Value, 'Estacion': Estacion, 'Tag': ACTUADOR, 'Value': Sensor}
        plc_data = plc_data.append(temp_dict.copy(), ignore_index=True)
        plc_data.to_excel("Actuadores.xlsx", sheet_name='Result', index=False)
        print(Sensor)

    else:
        Sensor = "ACTUADOR 2 WORK OK" 
        print(Sensor)
   


    ACTUADOR = comm.Read('D2M_150.Status.Act._2.HBypassed')
    ret = comm.GetPLCTime()
    if ACTUADOR.Value == True:
        Sensor = "ACTUADOR 2 HOME SENSOR EN BY PASS"
        Numero_Act_EnBypass= (Numero_Act_EnBypass+1)
        temp_dict = {'PLC IP': comm.IPAddress, 'Date Time': ret.Value, 'Estacion': Estacion, 'Tag': ACTUADOR, 'Value': Sensor}
        plc_data = plc_data.append(temp_dict.copy(), ignore_index=True)
        plc_data.to_excel("Actuadores.xlsx", sheet_name='Result', index=False)
        print(Sensor)

    else:
        Sensor = "ACTUADOR 2 HOME OK" 
        print(Sensor)

    print("Terminado")
    print("Actuadores en Bypass:", Numero_Act_EnBypass)
    comm._closeConnection
Reply
#2
I don't know. What is it doing wrong? Does it report an error? Does it not do what it is supposed to do? More details please.
Reply
#3
It has no errores

the problem is that when it finish the SE1_F2 (or any other) it closes all
and i need that when the script finish, go back to the main script and validate the next IP
Reply
#4
The reason I ask is because your program is written to stop running after the first successful connection.
If I can talk to PLC_1
    configure PLC_1
else
    If I can talk to PLC_2
        configure PLC_2
You can only get to PLC_2 if the connection to PLC_1 fails. Instead you should call them like this.
If I can connect to PLC_1
    configure PLC_1
If I can connect to PLC_2
    configure PLC_2
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python openyxl not updating Excel file MrBean12 1 336 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Python logging RotatingFileHandler writes to random file after the first log rotation rawatg 0 409 Feb-15-2024, 11:15 AM
Last Post: rawatg
  Is possible to run the python command to call python script on linux? cuten222 6 733 Jan-30-2024, 09:05 PM
Last Post: DeaD_EyE
  connect sql by python using txt. file dawid294 2 437 Jan-12-2024, 08:54 PM
Last Post: deanhystad
  How to check if Skype call is connected or disconnected in Python? boral 1 400 Dec-28-2023, 08:31 AM
Last Post: buran
  file open "file not found error" shanoger 8 1,129 Dec-14-2023, 08:03 AM
Last Post: shanoger
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,455 Nov-09-2023, 10:56 AM
Last Post: mg24
  Replace a text/word in docx file using Python Devan 4 3,412 Oct-17-2023, 06:03 PM
Last Post: Devan
  Help creating shell scrip for python file marciokoko 10 1,366 Sep-16-2023, 09:46 PM
Last Post: snippsat
  Need to replace a string with a file (HTML file) tester_V 1 770 Aug-30-2023, 03:42 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020