howdoesthiswork
howdoesthiswork

Reputation: 61

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.

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

Upvotes: 0

Views: 88

Answers (2)

midor
midor

Reputation: 5557

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.

Upvotes: 2

Moinuddin Quadri
Moinuddin Quadri

Reputation: 48120

You may use subprocess.check_output() which runs command with arguments and return its output as a byte string. 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. Your check_output call should be like:

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. Use Popen with the communicate() method when you need a stderr pipe.

Upvotes: 1

Related Questions