It is not clear what your exact problem is. What did you get and what did you expect? (A better formatting would also help.)
Is that all the code you have? If not, what is missing? I guess you defined pctl somewhere, what is the definition? If not, your code will most likely not work because pctl is not defined.
Uploading the complete script you use and posting the KiCad version would help us.
This is a much more complicated and complex option that needs a large amount of bloat, i wouldn’t use it when there is a Python API built in KiCad that can do the job. The KiBot can do many things the Python API can’t do (not sure if this is still correct for version 7), but as long as the Python API is good enough, why use KiBot?
Because it’s a lot easier to run one pip install command and one kibot --someflag command than figuring out kicad api quirks. Granted you have to figure out --someflag too but kibot docs are much better and searchable unlike kicad api docs.
If learning api in itself isn’t the goal then just use the wheel someone built instead of reinventing it. Just my opinion.
Then there’s kicad-cli which comes bundled with kicad. I haven’t used it so I can’t say if it helps, but it’s worth looking at. It can be used from a script, of course.