为forloop创建列表

2024-06-16 14:00:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个脚本,它创建了许多到VMs的控制台链接(内置了一个html主体),以便更快地访问,控制台链接由许多字符串和变量组合在一起。你知道吗

我想创建一个包含所有创建的链接的列表。你知道吗

不管怎样,我已经创建了所有链接的列表,丢失了脚本的概述…


Tags: 字符串脚本列表链接html内置主体vms
2条回答

可能需要修改脚本以将链接存储在数据结构(如列表)中:

links = list()
...
# iteration happens here
link =  'https://' + host + ':' + console_port + '...'
print link
links.append(link)
# script done here; return or use links

最后,您可以返回/使用您收集的所有链接的列表。你知道吗

您可以使用^{}来运行带参数的命令,并以字节字符串的形式返回其输出。例如:

>>> import subprocess
>>> my_var = subprocess.check_output(["echo", "Hello"])
>>> my_var
'Hello\n'

在这种情况下,您有一个可执行文件,比如my_script.py,它接收param1param2作为参数。您的check_output调用应该如下所示:

my_output = subprocess.check_output(["./my_script.py", "param1", "param2"])

根据文件:

Note: Do not use stderr=PIPE with this function as that can deadlock based on the child process error volume. Use Popen with the communicate() method when you need a stderr pipe.

相关问题 更多 >