Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
  #1  
Old July 20th, 2003, 07:34 AM
sissy sissy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 29 sissy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 48 m 25 sec
Reputation 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.

Reply With Quote
  #2  
Old July 20th, 2003, 08:40 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #3  
Old July 20th, 2003, 09:49 AM
sissy sissy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 29 sissy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 48 m 25 sec
Reputation Power: 0
Re: append..

Quote:
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...

Reply With Quote
  #4  
Old July 20th, 2003, 01:05 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,430 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 21 h 41 m 55 sec
Reputation Power: 784
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

Puzzle of the Month solved by sizeablegrin, etienne141 and L7Sqr, superior C/C++ programmers of the month

Reply With Quote
  #5  
Old July 20th, 2003, 07:07 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #6  
Old July 20th, 2003, 07:19 PM
sissy sissy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 29 sissy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 48 m 25 sec
Reputation Power: 0
thanks Scorpions4ever

thats exactly what i was looking for...

so simple yet no idea

thanks again folks

Reply With Quote
  #7  
Old July 20th, 2003, 07:32 PM
sissy sissy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 29 sissy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 48 m 25 sec
Reputation 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

Reply With Quote
  #8  
Old July 21st, 2003, 12:44 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #9  
Old July 21st, 2003, 06:32 PM
telex4's Avatar
telex4 telex4 is offline
Wacky hack
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2001
Location: London, England
Posts: 512 telex4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 29 sec
Reputation Power: 8
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'

Reply With Quote
  #10  
Old July 21st, 2003, 08:17 PM
sissy sissy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 29 sissy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 48 m 25 sec
Reputation Power: 0
Quote:
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...

Reply With Quote
  #11  
Old July 22nd, 2003, 05:03 AM
telex4's Avatar
telex4 telex4 is offline
Wacky hack
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2001
Location: London, England
Posts: 512 telex4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 29 sec
Reputation Power: 8
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?

Reply With Quote
  #12  
Old July 23rd, 2003, 11:15 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Sure thing sissy my email is netytan at hotmail dot com. looking forward to hearing about it.

Take care,
Mark.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > for or while loop? help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump