简体   繁体   English

为循环创建列表

[英]create list for for-loop

I have a script that creates a number of console links (with an html body built around) to VMs for faster access, the console links are put together by a number of strings and variables. 我有一个脚本,该脚本可创建到VM的多个控制台链接(带有html主体)以加快访问速度,这些控制台链接由许多字符串和变量组合在一起。

I want to create a list the contains all the links created. 我想创建一个包含所有已创建链接的列表。

Nevermind I already created a list of all links, lost the overview of my script ... 没关系,我已经创建了所有链接的列表,丢失了脚本的概述...

Probably you would want to modify your script to store the links in a data-structure like a list: 可能您想修改脚本以将链接存储在列表等数据结构中:

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

In the end you can then return/use the list of all links you collected. 最后,您可以返回/使用收集到的所有链接的列表。

You may use subprocess.check_output() which runs command with arguments and return its output as a byte string. 您可以使用subprocess.check_output()来运行带参数的命令,并以字节字符串形式返回其输出。 For example: 例如:

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

In case, you are having a executable file, say my_script.py which receives param1 and param2 as argument. 如果您有一个可执行文件,例如my_script.py ,它将接收param1param2作为参数。 Your check_output call should be like: 您的check_output调用应类似于:

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

As per the document: 根据文档:

Note: Do not use stderr=PIPE with this function as that can deadlock based on the child process error volume. 注意:请勿将此功能与stderr = PIPE一起使用,因为这会根据子进程错误量而死锁。 Use Popen with the communicate() method when you need a stderr pipe. 需要stderr管道时,可以将Popen与communication()方法一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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