medoix
medoix

Reputation: 1179

Python3 Create Array from List (I Think)

I am writing a Python3 script to run on both windows/osx that will essentially unregister all VirtualBox machines.

To do this i plan on listing all currently registered machines and then looping through the output to unregister each.

The output of the command

VBoxManage list vms

is

"virtual-machine1" {a391df10-c90b-4dcb-b149-c739ddde3b2f}
"virtual-machine2" {5ed8d7a8-df6e-4f4d-8ccc-9aacba90bd66}
"virtual-machine3" {820c4977-0fd2-4d37-8fbf-5760b171dc2a}
"virtual-machine4" {9bbd5b02-ccb7-4fb6-b167-d3ec6a729490}
"virtual-machine5" {816fef2c-05a4-4acb-931c-47877de46547}
"virtual-machine6" {5f2f81ee-6414-4a28-aac6-4921439bfaea}
"virtual-machine7" {03aa7fe4-5c78-4c3a-ac1e-475b704e8449}

How would one convert each of the strings inside the double quotes into an array to then iterate over with a command to unregister?

This is what i have been trying

    existing = os.system("VBoxManage list vms")
    machines = re.findall(r'"([^"]*)"', existing)
    for m in machines:
        print(m)

But keep getting

TypeError: expected string or bytes-like object

Upvotes: 3

Views: 53

Answers (1)

Joey Zhang
Joey Zhang

Reputation: 348

The return value of os.system is not the stdout of the command, see pydoc. As a result, you could not use re on it.

You might want to use subprocess, as shown following:

with subprocess.Popen(["VBoxManage", "list", "vms"], stdout=PIPE) as proc:
    machines = re.findall(r'"([^"]*)"', (proc.stdout.read())
    ....

Upvotes: 1

Related Questions