【发布时间】:2022-10-06 07:43:37
【问题描述】:
我希望有人可以帮助我解决这个问题,因为我迷路了。 我正在调用一个产生多行输出的 Powershell 脚本,这在摘录中:
7-Zip 22.01 (x64) : Copyright (c) 1999-2022 Igor Pavlov : 2022-07-15
Scanning the drive:
7 folders, 21 files, 21544 bytes (22 KiB)
Creating archive: conf.tar
Creating archive: conf2.tar
Removing tar file after upload...
Generating Links:
--------------------------------------------------------------
Link_1
https://some-repository.s3.ap-northeast-2.amazonaws.com/test/conf.tar?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=XXXXXXXXXXXXXXXXXX..
--------------------------------------------------------------
Link_2
https://some-repository.s3.ap-northeast-2.amazonaws.com/test/conf2.tar?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=XXXXXXXXXXXXXXXXXX..
我的 Python 脚本以这种方式调用 Powershell 脚本:
import subprocess, sys
p = subprocess.Popen([\"powershell.exe\",
\"script.ps1\"],
stdout=sys.stdout, shell=True)
p_out, p_err = p.communicate()
print(p_out)
当我从 Powershell CLI 运行 python 脚本时,我可以在屏幕上看到输出。 有没有办法从输出中提取这些链接并将它们传递给 Python?
-
您应该将
p_out中的所有内容都作为字符串(因此您已经在 Python 中拥有它),现在您应该使用 Python\ 的函数从该字符串中提取它。您可以在开头使用https拆分为行和搜索行。或者你可以使用正则表达式。 -
@furas,问题是
stdout=sys.stdout(而不是stdout=subprocess.PIPE),它阻止p_out接收任何输出。
标签: python powershell