gpt4 book ai didi

python - 无法将参数从 ant build.xml 文件传递​​给 pythonscript。我怎样才能传递值?

转载 作者:行者123 更新时间:2023-12-04 03:56:00 24 4
gpt4 key购买 nike

我在运行 build.xml 文件时尝试从我的 ant build.xml 文件运行 python 程序,但我无法将参数传递给 python 脚本

检查给定数字的阶乘的 Python 程序:

事实.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))

build.xml 文件如下所示:

<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>

当运行 build.xml 文件时,它会运行 fact.py python 程序,并且它期望用户输入来检查给定数字的阶乘。

如何将数字从 ant build.xml 文件传递​​给 python 程序

提前致谢!!!!!!

最佳答案

根据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)

那你能做什么?提供以下之一:

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.


例子:

<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>

关于python - 无法将参数从 ant build.xml 文件传递​​给 pythonscript。我怎样才能传递值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63897693/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com