What am I doing wrong here

Discussion in 'Python' started by Hitesh Joshi, Apr 24, 2006.

  1. Hitesh Joshi

    Hitesh Joshi Guest

    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"')
     
    Hitesh Joshi, Apr 24, 2006
    #1
    1. Advertisements

  2. Hitesh Joshi

    Hitesh Joshi Guest

    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"')
     
    Hitesh Joshi, Apr 24, 2006
    #2
    1. Advertisements

  3. Hitesh Joshi

    Robert Kern Guest

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

    --
    Robert Kern


    "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
     
    Robert Kern, Apr 24, 2006
    #3
  4. Hitesh Joshi

    Hitesh Joshi Guest

    Thank you Robert, It worked!!!

    Thank you so much....
     
    Hitesh Joshi, Apr 24, 2006
    #4
  5. Hitesh Joshi

    Gary Herron Guest

    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
     
    Gary Herron, Apr 24, 2006
    #5
  6. Hitesh Joshi

    Hitesh Joshi Guest

    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
     
    Hitesh Joshi, Apr 24, 2006
    #6
  7. Hitesh Joshi a écrit :
    (snip)
    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>
     
    Bruno Desthuilliers, Apr 25, 2006
    #7
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.