comparing elements of a list with a string

byte8bits at gmail.com byte8bits at gmail.com
Tue Sep 25 17:42:26 EDT 2007


On Sep 25, 11:39 am, Shriphani <shripha... at gmail.com> wrote:

> 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)

I would do something like this instead:

>>> for root, dirs, files in os.walk('.'):
...   for f in files:
...     if 'text' in f:
...       print f
...
gimp-text-tool
gimp-text-tool.presets
text.py~
textwrap.pyc
textwrap.py
...

You can append the output to a list and return that list if you want
to encapsulate this in a function.




More information about the Python-list mailing list