Remove a network printer with Python's wmi module?

1

For starters, here's the WMI module I'm referring to.

I've tried many combinations of code, and understand how to remove network printers with wmic from the command line, as well as a basic understanding on how to remove network printers with wmi in VB, etc. but I still can't figure out how to do so in this python module.

Does anyone have experience with this? I'm testing with PyDev in Eclipse, typically on a Windows 7 machine (which this program will be used for along with XP), but also on Windows 8.

Here's some code I've tried:

import wmi
c = wmi.WMI ()

c.win32_printer("\\\\server\\printer").delete

And I get the following error:

wmi.x_wmi_invalid_query: <x_wmi: Unexpected COM Error (-2147217385, 'OLE error 0x80041017', None, None)>
python
windows
printing
wmi
python-module
asked on Stack Overflow Jul 11, 2013 by EpicCyndaquil • edited Jul 11, 2013 by EpicCyndaquil

1 Answer

1

A friend (who probably wishes to remain unnamed) found a solution!

for printer in c.win32_printer():
    if printer.DEVICEID == "\\\\server\\printer":
    printer.delete_()

For some reason, the server name (and possibly printer name) seem to be case-sensitive, so keep an eye out for that. I'd guess it's because python is case sensitive, and it's comparing via python, not WMI.

answered on Stack Overflow Jul 11, 2013 by EpicCyndaquil

User contributions licensed under CC BY-SA 3.0