What am I doing wrong here

H

Hitesh Joshi

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"')
 
H

Hitesh Joshi

ok here is the deal... I figured out how to pass the variable but now
messages are not popping up on the windows screen if I use this method:

import os

Computerlist = ['PC1', 'PC2', 'PC3', 'PC4', 'PC5']
for ComputerName in Computerlist:
print ComputerName
s = "net send %s" % ComputerName
os.system('s "Message"')
 
R

Robert Kern

Hitesh said:
ok here is the deal... I figured out how to pass the variable but now
messages are not popping up on the windows screen if I use this method:

import os

Computerlist = ['PC1', 'PC2', 'PC3', 'PC4', 'PC5']
for ComputerName in Computerlist:
print ComputerName
s = "net send %s" % ComputerName
os.system('s "Message"')

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

--
Robert Kern
(e-mail address removed)

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
G

Gary Herron

Hitesh said:
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
Gary Herron
 
H

Hitesh Joshi

Thank you all for the quick replies. It worked! Truely appriciated. I
am python novice and still learning.... I hope to contribute to this
group someday :)

Hitesh
 
B

Bruno Desthuilliers

Hitesh Joshi a écrit :
(snip)
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"')
What you pass to os.system is the litteral string 'net send ComputerName
"Message"'. This string is passed 'as is' - the fact that it contains
'ComputerName' won't invoke any magic...

Try this instead:
Computerlist = ['PC1', 'PC2', 'PC3', 'PC4', 'PC5']
for ComputerName in Computerlist:
os.system('net send %s "Message"' % ComputerName)

<ot>
In Python, CapitalizedNames are usually used for class names. You'd
better use all_lowers_with_underscores, or at least mixedCase.
</ot>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top