What am I doing wrong here

Gary Herron gherron at islandtraining.com
Mon Apr 24 18:40:04 EDT 2006


Hitesh Joshi wrote:

>Hi,
>
>I wanted to pass a popup mesage using windows messagin service to five
>PCs.
>If I just use following then PC1 gets the popup service message:
>
>import os
>os.system('net send PC1  "Message"')
>
>
>But if I try to create a for loop like this it doesn't work.... how can
>I pass computerName var as an argument?
>What am I doing wrong here? Thank you in advance....
>
>import os
>
>Computerlist = ['PC1', 'PC2', 'PC3', 'PC4', 'PC5']
>for ComputerName in Computerlist:
>    print ComputerName
>    os.system('net send ComputerName  "Message"')
>
>  
>
Well... Just look at the name of the computer you are sending the 
message to.    Its  looking for a computer named 'ComputerName', not 
'PC1' ...

You want to create a command that has the computer's name in it, like 
this: 'net send PC1', not like this 'net send ComputerName'.  You have 
several ways to from such a string.  You have the same problem with the 
message.  Your message will be the string 'Message' not the contents of 
a variable names Message.  Try:

    os.system('net send %s "%s"' % (ComputerName, Message))

(where the % operator replaces %s's on the left with values taken from the variables on the right)

or

    os.system('net send ' + ComputerName + ' "' + Message + '"')

where the +'s build the command string up from pieces.

You might try invoking Python interactively and try typing some of these 
expressions by hand to see that happens:

python
 >>> ComputerName = 'Fred'
 >>> Message = 'HI'
 >>> print 'net send ComputerName "Message"'
net send ComputerName "Message"
 >>> print 'net send %s "%s"' % (ComputerName, Message)
net send Fred "HI"
 >>>

Gary Herron








More information about the Python-list mailing list