|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
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
|
|||
|
|||
|
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:
Many thanks and lool forward to helping you guys some day. |
|
#2
|
||||
|
||||
|
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. |
|
#3
|
|||
|
|||
|
Re: append..
Quote:
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... |
|
#4
|
||||
|
||||
|
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 |
|
#5
|
||||
|
||||
|
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. |
|
#6
|
|||
|
|||
|
thanks Scorpions4ever
thats exactly what i was looking for... so simple yet no idea ![]() thanks again folks |
|
#7
|
|||
|
|||
|
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 |
|
#8
|
||||
|
||||
|
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. |
|
#9
|
||||
|
||||
|
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'
|
|
#10
|
|||
|
|||
|
Quote:
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... |
|
#11
|
||||
|
||||
|
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? ![]() |
|
#12
|
||||
|
||||
|
Sure thing sissy my email is netytan at hotmail dot com. looking forward to hearing about it.
Take care, Mark. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > for or while loop? help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|