2
votes

Using python import os and os.system("create-object "+ more-specifications), I created a few hundreds of objects, which I now need to delete. I can list the objects created, including their unique id.

To delete just one of them, on the command line, I issue

delete-object --id cfa2d1417633

which will ask for confirmation with

Are you sure you want to delete that object (y or n)?> 

to which I then respont with y.

I can generate a list of the id's to delete, but I can't programatically delete them because I don't know how to respond. This, for instance won't work:

for objectSpecification in onbjectList:
    os.system("delete-object --id "+ objectSpecification["id"])
    os.system('n')

The delete will not happen and te 'n' causes an error 'n' is not recognized as an internal or external command, operable program or batch file.

1
You could use popen instead: p = os.popen(command, "w") and then p.write("y\n") - user56700
Thanks If you post this as an answer, I will accept and upvote it. This would help others with similar problems finding the sollution. - Dirk Horsten
Maybe delete-object accepts a parameter to delete without confirmation, e.g. delete-object -y OBJECT or delete-object -f OBJECT - Mark Setchell
Maybe you can pipe a y into it via your shell, e.g. echo "y" | delete-object OBJECT - Mark Setchell
That probably works, but the solution from user56700 is more flexible, though it is more verbosse - Dirk Horsten

1 Answers

1
votes

Converting to an answer from comment.

Suggestion to a solution, using popen:

import os

onbjectList = []

def deleteObject(objid):
    with os.popen(f"delete-object --id {objid}", "w") as p:
        p.write("y\n")

for objectSpecification in onbjectList:
    deleteObject(objectSpecification['id'])