0

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

  • Assign it to a variable *in the console*? Or assign all `print` output to a variable *in Python*? The sane way to do that is to separate data from presentation: your script can generate a `list` or such of URLs, and another function can format that to HTML. That way you can use the data either way… – deceze Oct 18 '16 at 10:10
  • 1
    Possible duplicate of [Any way to assign terminal output to variable with python?](http://stackoverflow.com/questions/2449250/any-way-to-assign-terminal-output-to-variable-with-python) – Ari Gold Oct 18 '16 at 10:13

2 Answers2

2

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.

midor
  • 5,487
  • 2
  • 23
  • 52
  • I think this is what I'm looking for. I forgot to mention in my question that I defined a function which goes through folders and creates the link. – howdoesthiswork Oct 18 '16 at 11:47
  • i forgot the important part in the comment before ... The for-loop then uses the function to generate the link for the existing data (I included the structure in my original question – howdoesthiswork Oct 18 '16 at 11:49
1

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.

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126