简体   繁体   中英

Unable to pass parameters to pythonscript from ant build.xml file. How can i pass the value?

I am trying to run a python program from my ant build.xml file when running the build.xml file i am unable to pass arguments to python script

Python program to check factorial of a given number:

fact.py

#!/usr/bin/python

def factorial(num):
    if num == 1:
        return num
    else:
        return num * factorial(num - 1)

 num = int(input('Enter a Number: '))
 if num < 0:
     print 'Factorial cannot be found for negative numbers'
 elif num == 0:
     print 'Factorial of 0 is 1'
 else:
     print ('Factorial of', num, 'is: ', factorial(num))

The build.xml file looks like as below:

<project name="ant_test" default="python" basedir=".">
<target name="python" >
        
        <exec dir="D:\app\python" executable="D:\app\python\python.exe" failonerror="true">
            <arg line="D:\app\ant-workout\fact.py/>
        </exec>
</target>

when running the build.xml file it runs the fact.py python program and it was expecting a user input to check the factorial of a given number .

How can i pass the number to the python program from ant build.xml file

Thanks in Advance!!!!!

According to the documentation

Note that you cannot interact with the forked program, the only way to send input to it is via the input and inputstring attributes. Also note that since Ant 1.6, any attempt to read input in the forked program will receive an EOF (-1). This is a change from Ant 1.5, where such an attempt would block.
(emphasis mine)

So what can you do? Provide either of:

input
A file from which the executed command's standard input is taken. This attribute is mutually exclusive with the inputstring attribute.

inputstring
A string which serves as the input stream for the executed command. This attribute is mutually exclusive with the input attribute.


Example:

<project name="ant_test" default="python" basedir=".">
<target name="python" >
        
        <exec dir="D:\app\python" executable="D:\app\python\python.exe" 
                                  failonerror="true" 
                                  inputstring="42">
            <arg line="D:\app\ant-workout\fact.py/>
        </exec>
</target>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM