The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
for or while loop? help
Discuss for or while loop? help in the Python Programming forum on Dev Shed. for or while loop? help Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

July 20th, 2003, 07:34 AM
|
|
Registered User
|
|
Join Date: Jul 2003
Posts: 29
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.
|

July 20th, 2003, 08:40 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|
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.
|

July 20th, 2003, 09:49 AM
|
|
Registered User
|
|
Join Date: Jul 2003
Posts: 29
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...
|

July 20th, 2003, 01:05 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
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
|

July 20th, 2003, 07:07 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

July 20th, 2003, 07:19 PM
|
|
Registered User
|
|
Join Date: Jul 2003
Posts: 29
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
|

July 20th, 2003, 07:32 PM
|
|
Registered User
|
|
Join Date: Jul 2003
Posts: 29
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
|

July 21st, 2003, 12:44 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|
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.
|

July 21st, 2003, 06:32 PM
|
 |
Wacky hack
|
|
Join Date: Apr 2001
Location: London, England
Posts: 513
Time spent in forums: 1 h 38 m 37 sec
Reputation Power: 13
|
|
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'
|

July 21st, 2003, 08:17 PM
|
|
Registered User
|
|
Join Date: Jul 2003
Posts: 29
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...
|

July 22nd, 2003, 05:03 AM
|
 |
Wacky hack
|
|
Join Date: Apr 2001
Location: London, England
Posts: 513
Time spent in forums: 1 h 38 m 37 sec
Reputation Power: 13
|
|
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? 
|

July 23rd, 2003, 11:15 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|
Sure thing sissy my email is netytan at hotmail dot com. looking forward to hearing about it.
Take care,
Mark.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|