To avoid wasting any more of your time, here is the best way I have found to do it.
I will call the following Python script my_script.py from PowerShell, which simply prints out the arguments passed to Python:
import sys
for i in range(len(sys.argv)): print("my_script args: " + str(i) + ": " + str(sys.argv[i]))
The script is called from PowerShell by putting the Python command-line arguments into an array and passing them to the external script using the Splat operator.
# Put Python command-line arguments into an array
$cmd_args = @("c:\temp\my_script.py", "-f", "c:\myfile.txt", "-t", "5")
# Call the Python executable, supplying arguments using the Splat operator
& python.exe @cmd_args
my_script args: 0: c:\temp\my_script.pymy_script args: 1: -fmy_script args: 2: c:\myfile.txtmy_script args: 3: -tmy_script args: 4: 5
No comments:
Post a Comment