#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    29
    Rep Power
    0

    for or while loop? help


    Hi folks,

    how are we all... i just started to look at python as of yesterday and seems quite powerfull and am slowly learning

    one thing i hate and have always hated is double for loops ( thats what i think i need here) hoping someone can help me

    as the code stands at the moment as soon as a program is missing it exits. what i would like it to do is cycle thru the program list and print all the missing programs then exit only if 1 or more are missing

    PHP Code:
    #!/usr/bin/env python

    import os
    import sys

    # programs needed
    progs = ['reformime','unzip']

    # test for programs
    for prog in progs:
        if 
    os.system('which ' prog) != 0:
            print 
    'Could not find ' prog
            sys
    .exit()
        else:
            
    pass

    print 'next' 
    Many thanks and lool forward to helping you guys some day.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69

    append..


    Mmm ok so if i've got this right, you want to lop though and see which programs are on the system, and print them to the console?

    Why not have a list (array) for missing and found then append the names to the list instead of exiting. This will give you a present and a not present block when you print it out.

    If you dont wana use a loop you could use the string modules join function on the two lists, or if your using python 2+..

    Code:
    'chat to join with'.join(list)
    Hope this helps, if you have any querstions feel free to ask.

    Have fun,
    Mark.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    29
    Rep Power
    0

    Re: append..


    Originally posted by netytan
    Mmm ok so if i've got this right, you want to lop though and see which programs are on the system, and print them to the console?
    Hi Mark,

    thanks for the reply, your assumption is correct and thats what it does now.

    I want it to exit as i dont wont the program to continue unless the files are present and storing whats there and whats not as you said i think is an over kill for what i want and probably too hard (for me anyways).

    i think i need some sort of counter so it counts how many files there are to be checked then if any of those files are not present print which ones arent and exit otherwise continue.

    Hope that sheds some more light on what im looking for

    thanks again Mark...
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,595
    Rep Power
    4207
    Code:
    #!/usr/bin/python                                                                                                                   
    import os;
    import sys;
    
    # programs needed                      
    progs = ['reformime','unzip']
    
    # test for programs                       
    missing = 0
    for prog in progs:
        if os.system('which ' + prog) != 0:
            print 'Could not find ' + prog
            missing = missing + 1
        else:
            pass
    
    if (missing > 0):
        print "We have some missing programs."
        sys.exit(1)
    
    print 'next'
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Sweet,

    just do what scorpy said looks like it does what you want. Should exit if any of the programs are missing, printing each missing program to the console as it's check. If I can ask, what system is this running on as "which program_name" definatly isn't a windows thing so probably *nix. Also whats this for, just out of interest.

    Have fun,
    Mark.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    29
    Rep Power
    0
    thanks Scorpions4ever

    thats exactly what i was looking for...

    so simple yet no idea

    thanks again folks
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    29
    Rep Power
    0
    Hi Mark,

    Yeh its on freebsd 5.1, the script i am hoping to make will be a pipe from "getmail" to scan for email attachments, then unpack any compressed ones then scan for viruses using nod32

    I can see it will be a slow process coding it but im going to give it a go with some help from here of course...

    I can here it now why not use fetchmail and qmail-scanner. Well i was using that setup but was tired of fetchmails mail retrieval technique and also the reinjection process back into to qmail.

    So thats why i swapped and in doing so i havent really found a script for "getmail" to do what i am doing at the moment, so what better way to try something new is code one myself and in an unknown language to me.

    your quite welcome to help

    cya
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Sounds interesting, i would be happy to help. I do have a few projects of my own at the moment but feel free to count me in. if i have any free time, ill work on it. Would like more info though, how far you are, that steps you want to take/have taken and etc. Also i dont have freebsd, this a problem???

    Thanks,
    Mark.
  16. #9
  17. Wacky hack
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Location
    London, England
    Posts
    513
    Rep Power
    14
    I'm guessing that so long as you have a UNIX-like system (FreeBSD, OpenBSD, GNU/Linux, etc.) you'll be able to use it and help out.

    Oh, and because variety is the spice of life:

    Code:
    #!/usr/bin/env python
    import os, sys
    
    progs = ['reformime','unzip']
    paths = os.environ['PATH'].split(":")
    
    # test for programs
    for prog in progs:
      for path in paths:
        if os.path.isfile(os.path.join(path, prog)):
            pass
        else:
            print "Unable to find %s" % (prog)
            sys.exit(1)
    
    print 'next'
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    29
    Rep Power
    0
    Originally posted by telex4
    I'm guessing that so long as you have a UNIX-like system (FreeBSD, OpenBSD, GNU/Linux, etc.) you'll be able to use it and help out.

    Oh, and because variety is the spice of life:

    Code:
    #!/usr/bin/env python
    import os, sys
    
    progs = ['reformime','unzip']
    paths = os.environ['PATH'].split(":")
    
    # test for programs
    for prog in progs:
      for path in paths:
        if os.path.isfile(os.path.join(path, prog)):
            pass
        else:
            print "Unable to find %s" % (prog)
            sys.exit(1)
    
    print 'next'
    Hi telex4,

    nice code but unfortantly it doesnt work and heres what i see happening...

    this " paths = os.environ['PATH'].split(":") " grabs all our paths /sbin /bin /usr/bin etc

    now " if os.path.isfile(os.path.join(path, prog)): " is joining the path to our binary but unfortantly its first test is /sbin/reformime which does not exist so it fails instantly?

    i would of thought it would of cycled thru all of them and if NONE were found then fail, but this not the case... unless ive misunderstood

    thanks anyway telex4 sure would of been nice and once complete and BUG free ill submit to getmail author or somewhere for everyone to use.

    -----------

    Mark i will email you if you dont mind and explain all there it wont matter that your on windows as i can do the BSD stuff its just the python that will be the issue


    thanks all and sorry for the rambling...
  20. #11
  21. Wacky hack
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Location
    London, England
    Posts
    513
    Rep Power
    14
    Oh, oops, heh Just goes to show... you should test code before posting Here's an amended version:

    Code:
    #!/usr/bin/env python
    import os, sys
    
    progs = ['reformime','unzip']
    paths = os.environ['PATH'].split(":")
    
    # test for programs
    for prog in progs:
      found = 0
      for path in paths:
        if os.path.isfile(os.path.join(path, prog)):
            found = 1
    	break
        else:
            pass
      if found is 0:
        print "Unable to find %s" % (prog)
        sys.exit(1)
    
    print 'next'
    How's that?
  22. #12
  23. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Sure thing sissy my email is netytan at hotmail dot com. looking forward to hearing about it.

    Take care,
    Mark.

IMN logo majestic logo threadwatch logo seochat tools logo