0

To run a SAP GUI data extraction script, I am comparing the modification time of a file with the current time with the for loop, to find out if the file is update. But the loop is infinite, and is not respecting the conditional of the for loop.

I need help with this. I'm using python 3.6x

today = datetime.datetime.today().strftime('%d.%m.%Y')
GUIPath = 'C:\Program Files (x86)\SAP\FrontEnd\SAPgui'
SID = '-system=x'
InstanceNo = '-client=x'
User = '-user=x'
Pw = '-pw=x'
Language = '-language=x'
shell = win32com.client.Dispatch("WScript.Shell")
cmdString = os.path.join(GUIPath, 'sapshcut.exe') + " " + SID + " " + InstanceNo + " " + User + " " + Pw + " " + Language

base_sap = (r'\\teste.com\JK1\Openshare\Groups\Kb_CDO_Informacoes\Tableau\Pedidos_Ingressados\Files\Teste\base_sap.xlsx')
now  = datetime.datetime.now().timestamp()

for f in base_sap:
    while os.stat(f).st_mtime < (now - 1800):
        print('Iniciando atualização da Base de Pedidos do SAP.')
        #inicia o sap logon
        call(cmdString)
        time.sleep(4)
        os.system('TASKKILL /F /IM saplogon.exe')
        call(cmdString)
        time.sleep(10)
        #iniciando a sessão no sap gui
        SapGui = win32com.client.GetObject("SAPGUI").GetScriptingEngine
        session = SapGui.FindById("ses[0]")
        time.sleep(8)
        #sap gui script
        session.findById("wnd[0]").maximize()
        session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").doubleClickNode("F00002")
        session.findById("wnd[0]/usr/tabsTABSTRIP_MYTABSTRIP/tabpCOMM_001/ssub%_SUBSCREEN_MYTABSTRIP:Z0OTC0718_SALES_ORD_REP:0100/ctxtS_VKORG-LOW").text = "BR03"
        session.findById("wnd[0]/usr/tabsTABSTRIP_MYTABSTRIP/tabpCOMM_001/ssub%_SUBSCREEN_MYTABSTRIP:Z0OTC0718_SALES_ORD_REP:0100/ctxtS_VKORG-HIGH").text = "BR04"
        session.findById("wnd[0]/usr/tabsTABSTRIP_MYTABSTRIP/tabpCOMM_001/ssub%_SUBSCREEN_MYTABSTRIP:Z0OTC0718_SALES_ORD_REP:0100/ctxtS_WERKS-LOW").text = "*"
        session.findById("wnd[0]/usr/tabsTABSTRIP_MYTABSTRIP/tabpCOMM_001/ssub%_SUBSCREEN_MYTABSTRIP:Z0OTC0718_SALES_ORD_REP:0100/ctxtS_AUART-LOW").text = "ZOR"
        session.findById("wnd[0]/usr/tabsTABSTRIP_MYTABSTRIP/tabpCOMM_001/ssub%_SUBSCREEN_MYTABSTRIP:Z0OTC0718_SALES_ORD_REP:0100/ctxtS_ERDAT-LOW").text = today
        session.findById("wnd[0]/usr/tabsTABSTRIP_MYTABSTRIP/tabpCOMM_001/ssub%_SUBSCREEN_MYTABSTRIP:Z0OTC0718_SALES_ORD_REP:0100/ctxtS_ERDAT-HIGH").setFocus()
        session.findById("wnd[0]/usr/tabsTABSTRIP_MYTABSTRIP/tabpCOMM_001/ssub%_SUBSCREEN_MYTABSTRIP:Z0OTC0718_SALES_ORD_REP:0100/ctxtS_ERDAT-HIGH").caretPosition = 0
        session.findById("wnd[0]/usr/tabsTABSTRIP_MYTABSTRIP/tabpCOMM_003").select()
        session.findById("wnd[0]/usr/tabsTABSTRIP_MYTABSTRIP/tabpCOMM_003/ssub%_SUBSCREEN_MYTABSTRIP:Z0OTC0718_SALES_ORD_REP:0300/ctxtP_UNIT").text = "L"
        session.findById("wnd[0]/usr/tabsTABSTRIP_MYTABSTRIP/tabpCOMM_003/ssub%_SUBSCREEN_MYTABSTRIP:Z0OTC0718_SALES_ORD_REP:0300/ctxtP_UNIT").setFocus()
        session.findById("wnd[0]/usr/tabsTABSTRIP_MYTABSTRIP/tabpCOMM_003/ssub%_SUBSCREEN_MYTABSTRIP:Z0OTC0718_SALES_ORD_REP:0300/ctxtP_UNIT").caretPosition = 1
        session.findById("wnd[0]/usr/tabsTABSTRIP_MYTABSTRIP/tabpCOMM_002").select()
        session.findById("wnd[0]/usr/tabsTABSTRIP_MYTABSTRIP/tabpCOMM_002/ssub%_SUBSCREEN_MYTABSTRIP:Z0OTC0718_SALES_ORD_REP:0200/ctxtP_VAR").text = "PEDIDOS_INGR"
        session.findById("wnd[0]/usr/tabsTABSTRIP_MYTABSTRIP/tabpCOMM_002/ssub%_SUBSCREEN_MYTABSTRIP:Z0OTC0718_SALES_ORD_REP:0200/txtP_TITLE").text = ""
        session.findById("wnd[0]/usr/tabsTABSTRIP_MYTABSTRIP/tabpCOMM_002/ssub%_SUBSCREEN_MYTABSTRIP:Z0OTC0718_SALES_ORD_REP:0200/txtP_TITLE").setFocus()
        session.findById("wnd[0]/usr/tabsTABSTRIP_MYTABSTRIP/tabpCOMM_002/ssub%_SUBSCREEN_MYTABSTRIP:Z0OTC0718_SALES_ORD_REP:0200/txtP_TITLE").caretPosition = 0
        session.findById("wnd[0]/tbar[1]/btn[8]").press()
        #exporta os dados em xlsx via automação pyautogui
        time.sleep(6)
        pyautogui.click(15, 32)
        pyautogui.click(57, 106)
        pyautogui.click(291, 126)
        time.sleep(6)
        pyautogui.press('enter')
        time.sleep(14)
        #encerra a aplicação excel
        os.system('TASKKILL /F /IM EXCEL.EXE')
        #encerra a aplicação sap logon
        os.system('TASKKILL /F /IM saplogon.exe')
        #copia o arquivo para o diretório compartilhado
        shutil.move(r"C:\Users\%s\Documents\SAP\export.XLSX"  % os.getlogin(), r"\\teste.com\JK1\Openshare\Groups\Kb_CDO_Informacoes\Tableau\Pedidos_Ingressados\Files\Teste\base_sap.xlsx")
    else:
        print('Base atualizada. Iniciando upload para o Tableau')
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
  • That means your condition isn't evaluating to what you think it does. There's never a case where the condition is falsey, but the loop continues. – Carcigenicate Feb 14 '18 at 16:50
  • Your `while` loop is not inside your `for` loop – Joe Feb 14 '18 at 16:51
  • What is supposed to be modifying `f` within the while loop? How is it supposed to reach a state when the condition is false? – Daniel Roseman Feb 14 '18 at 16:52
  • @DanielRoseman if the conditional is true the last line of the script overrides the previous file. Changing the modification time. – Gabriel Gramacho Feb 14 '18 at 17:17
  • 1
    But `base_sap` is a single string. Iterating through a string gives individual characters. What's the point of the for loop at all? – Daniel Roseman Feb 14 '18 at 17:26
  • @DanielRoseman I was just about to note that; I believe it's supposed to be a tuple of strings. Gabriel, again, make sure your code above works and reproduces your problem, according to [mcve]. (Removing some irrelevant fluff and making the code runnable by third parties wouldn't hurt either). – Andras Deak -- Слава Україні Feb 14 '18 at 17:27

0 Answers0