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
JavaScript
x
2
1
delete-object --id cfa2d1417633
2
which will ask for confirmation with
JavaScript
1
2
1
Are you sure you want to delete that object (y or n)?>
2
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:
JavaScript
1
4
1
for objectSpecification in objectList:
2
os.system("delete-object --id "+ objectSpecification["id"])
3
os.system('n')
4
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.
Advertisement
Answer
Converting to an answer from comment.
Suggestion to a solution, using popen:
JavaScript
1
11
11
1
import os
2
3
objectList = []
4
5
def deleteObject(objid):
6
with os.popen(f"delete-object --id {objid}", "w") as p:
7
p.write("yn")
8
9
for objectSpecification in objectList:
10
deleteObject(objectSpecification['id'])
11