python - 尝试使用 stdin : io. UnsupportedOperation 时在 Python 中出现错误:fileno

标签 python python-3.x subprocess

我正在尝试在 xml 文件中进行 std。我从颠覆中读取了 xml 文件,更新了文件中的一行,现在我正在尝试使用 subporcess.Popen 和 stdin 创建一个 jenkins 作业

test = subprocess.Popen('svn cat http://localhost/svn/WernerTest/JenkinsJobTemplates/trunk/smartTemplate.xml --username admin --password admin', stdout=subprocess.PIPE, universal_newlines=True)
job = test.stdout.read().replace("@url@", "http://localhost/svn/WernerTest/TMS/branches/test1")
output = io.StringIO()
output.write(job)
subprocess.Popen('java -jar D:\\applications\\Jenkins\\war\\WEB-INF\\jenkins-cli.jar\\jenkins-cli.jar -s http://localhost:8080/ create-job test7', stdin=output)

我收到以下错误:

Traceback (most recent call last):  File "D:\scripts\jenkinsGetJobs.py", line 20, in <module>
subprocess.Popen('java -jar D:\\applications\\Jenkins\\war\\WEB-INF\\jenkins-cli.jar\\jenkins-cli.jar -s http://localhost:8080/ create-job test7', stdin=output)
File "D:\applications\Python 3.5\lib\subprocess.py", line 914, 
in __init__errread, errwrite) = self._get_handles(stdin, stdout, stderr)
File "D:\applications\Python 3.5\lib\subprocess.py", line 1127, in _get_handles
p2cread = msvcrt.get_osfhandle(stdin.fileno())
io.UnsupportedOperation: fileno

那么如何将更新后的文件传递给下一个子进程呢?

最佳答案

使用管道并将数据直接写入该管道:

test = subprocess.Popen(
    'svn cat http://localhost/svn/WernerTest/JenkinsJobTemplates/trunk/smartTemplate.xml --username admin --password admin',
    stdout=subprocess.PIPE, universal_newlines=True)
job = test.stdout.read().replace("@url@", "http://localhost/svn/WernerTest/TMS/branches/test1")

jenkins = subprocess.Popen(
    'java -jar D:\\applications\\Jenkins\\war\\WEB-INF\\jenkins-cli.jar\\jenkins-cli.jar -s http://localhost:8080/ create-job test7',
    stdin=subprocess.PIPE, universal_newlines=True)
jenkins.communicate(job)

Popen.communicate() method接受第一个参数并将其作为标准输入发送到子进程。

请注意,我设置了 universal_newlines argument对于 Jenkins 也为 True;另一种方法是将 job 字符串显式编码为 Jenkins 将接受的合适的编解码器。

关于python - 尝试使用 stdin : io. UnsupportedOperation 时在 Python 中出现错误:fileno,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30171269/

相关文章:

python - 在 python 中使用 elementtree 提取 XML 节点文本时出错

python - Pandas 堆栈日期矩阵值

python - pytorch 中轴与数组不匹配错误

Python:如何将 ARGV 参数传递给另一个脚本?

python - 使用子进程 wait() 和 poll()

python numpy (v1.15.0) 无法拟合抛物线

python - Python 中数字之间的 AND/OR 运算符

python - 计算由另一列分组的列的 z 分数

python-3.x - 在超出范围的索引上打印 “no value”

如果需要多个标准输入,python asyncio 会死锁