Search code examples
pythonautoit

Execute external program with command line arguments


I want to execute a Python script from AutoIt using ShellExecuteWait(). My Attempt:

$x = ShellExecuteWait("E:/Automation/Python/Scripts/ReadLog.py", '-f "file.log" -k "key" -e "errMsg" ')
MsgBox(0,"x=",String($x))
If @error Then
    MsgBox(0,"Error=",String(@error))
EndIf

I can see some process id in $x, and @error also gets set to 0 (means AutoIt executed the script). But my Python script is not producing results (it writes to a txt file when executed independently). Seems the problem is with passing command line arguments like:

ShellExecuteWait("E:/Automation/Python/Scripts/ReadLog.py", '-f "file.log" -k "key" -e "errMsg" ')

How can I pass command line arguments using ShellExecuteWait()? Syntax:

ShellExecuteWait ( "filename" [, "parameters" [, "workingdir" [,"verb" [, showflag]]]] )

Parameters:

filename :- The name of the file to run (EXE, .txt, .lnk, etc).

parameters :- [optional] Any parameters for the program. Blank ("") uses none.

This misses examples for use of parameters. There are no problems with the Python script (it requires 3 command line arguments, strings with options -f, -k and -e).

Related: How to run or execute python file from autoit.


Solution

  • Check the path to your Python binary (e.g. Python.exe, wherever your Python program/binary is located) in your Window's system environment/path.

    Execute Python script from AutoIt If path is there then your code must work. In $x you will receive return exit code of the Python script.

    Also you can try:

    RunWait('full_path\Python.exe ReadLog.py -f "file.log" -k "key" -e "errMsg"', 'full_path_of_working_directory')