I am trying to make a simple printer removal script in Python 3.4.1. There are probably easier ways to do this, but this is what I have: I am using product from itertools
, taking input from two text files in a directory - one is a list of servers, one a list of printers.
The code I have right now is:
import webbrowser
from itertools import product
with open('servers.txt') as infile:
servers = infile.readlines()
with open('PRINTERS_toDelete.txt') as infile:
queues = infile.readlines()
strTemp1 = 'cscript c:\\windows\\system32\\prnmngr.vbs -d -p %s -s %s \n'
with open('RUN_THIS.txt', 'w') as outfile:
outfile.writelines(strTemp1 % (queue,server) for queue,server in product(queues,servers))
webbrowser.open('RUN_THIS.txt')
The output file is very close to what i need, but looks like this:
cscript c:\windows\system32\prnmngr.vbs -d -p queue1
-s server1
cscript c:\windows\system32\prnmngr.vbs -d -p queue1
-s server2
cscript c:\windows\system32\prnmngr.vbs -d -p queue1
-s server3
cscript c:\windows\system32\prnmngr.vbs -d -p queue2
-s server1
cscript c:\windows\system32\prnmngr.vbs -d -p queue2
-s server2
cscript c:\windows\system32\prnmngr.vbs -d -p queue2
-s server3
cscript c:\windows\system32\prnmngr.vbs -d -p queue3
-s server1
cscript c:\windows\system32\prnmngr.vbs -d -p queue3
-s server2
cscript c:\windows\system32\prnmngr.vbs -d -p queue3
-s server3
cscript c:\windows\system32\prnmngr.vbs -d -p iHatePrinters -s server1
cscript c:\windows\system32\prnmngr.vbs -d -p iHatePrinters -s server2
cscript c:\windows\system32\prnmngr.vbs -d -p iHatePrinters -s server3
where it should look like this:
cscript c:\windows\system32\prnmngr.vbs -d -p queue1 -s server1
cscript c:\windows\system32\prnmngr.vbs -d -p queue1 -s server2
cscript c:\windows\system32\prnmngr.vbs -d -p queue1 -s server3
cscript c:\windows\system32\prnmngr.vbs -d -p queue2 -s server1
cscript c:\windows\system32\prnmngr.vbs -d -p queue2 -s server2
cscript c:\windows\system32\prnmngr.vbs -d -p queue2 -s server3
cscript c:\windows\system32\prnmngr.vbs -d -p queue3 -s server1
cscript c:\windows\system32\prnmngr.vbs -d -p queue3 -s server2
cscript c:\windows\system32\prnmngr.vbs -d -p queue3 -s server3
cscript c:\windows\system32\prnmngr.vbs -d -p iHatePrinters -s server1
cscript c:\windows\system32\prnmngr.vbs -d -p iHatePrinters -s server2
cscript c:\windows\system32\prnmngr.vbs -d -p iHatePrinters -s server3
Basically, there's an unneeded line break between the queue and the server entry. Also the last entry seems to come out fine (or at least better than) the previous ones, so I think that it has something to do with the way it's interpreting a hard return in each of the input files. The additional formatting with no spaces for the same queue on different servers, and an extra line break between queues would be ideal but not necessary.
I've tried playing around with spacing a bit but had no luck -- any thoughts?
This happens because readlines method retains the final \n on each line of the file you're reading. Try this.
import webbrowser
from itertools import product
with open('servers.txt') as infile:
servers = list(map(str.strip, infile.readlines()))
with open('PRINTERS_toDelete.txt') as infile:
queues = list(map(str.strip,infile.readlines()))
strTemp1 = 'cscript c:\\windows\\system32\\prnmngr.vbs -d -p %s -s %s\n'
with open('RUN_THIS.txt', 'w') as outfile:
outfile.writelines(strTemp1 % (queue,server) for queue,server in product(queues,servers))
webbrowser.open('RUN_THIS.txt')