comparing elements of a list with a string
Larry Bates
larry.bates at websafe.com
Tue Sep 25 12:23:04 EDT 2007
Shriphani wrote:
> Hello all,
> I have a problem here. I have a list named list_of_files which
> contains filenames with their timestamps attached to the name. If I
> have a string "fstab", and I want to list out the files in whose names
> the word fstab appears should I go about like this :
>
> def listAllbackups(file):
> list_of_files = os.listdir("/home/shriphani/backupdir")
> for element in list_of_files:
> if element.find(file) != -1:
> date = ###
> time = ####
> return (date, time)
>
> The major trouble is that the return statement causes it to exit after
> attempt one. How do I use the yield statement here?
>
> Regards,
> Shriphani Palakodety
>
You should take a quick look at glob(). You may be able to use it to make life
a lot easier. Here is how you would do it if all your backup files begin with
fstab.
import glob
list_of_backup_files=glob.glob('/home/shriphani/backupdir/glob*')
If "fstab" can appear anywhere in the filename, this might not work for you.
-Larry
More information about the Python-list
mailing list