comparing elements of a list with a string

S

Shriphani

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
 
E

Evan Klitzke

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?

You can just replace the return statement with a yield statement,
nothing fancy is required. Also, as long as you're using the string.find
method to search for the file, I'd replace it with "if file in element",
which is a bit more idiomatic.
 
M

Matimus

Shriphani said:
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?

I would just do this:

Code:
from glob import glob
def listallbackupfiles(filename):
    return glob("/home/shriphani/backupdir/*%s*"%filename)
 
L

Larry Bates

Shriphani said:
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
 
S

Steve Holden

Larry said:
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.
I don't see why

list_of_backup_files=glob.glob('/home/shriphani/backupdir/*fstab*')

shouldn't work. However, t answer the OP's question about yield (which
nobody seems to have done fully yet):

1. Rewrite the function to become a generator function:

def listAllbackups(file):
list_of_files = os.listdir("/home/shriphani/backupdir")
for element in list_of_files:
if file in element: # tidied this up too
date = 1 ###
time = 2 ####
yield (date, time)

2. Create a generator by calling the function:

list_of_backup_files = listAllbackups("fstab")

3. Use the generator in an iterative context:

for file in list_of_backup_files:
# do something

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden

Sorry, the dog ate my .sigline
 
B

byte8bits

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 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.
 
S

Shriphani

Hello,
Would that mean that if I wanted to append all the (date, time) tuples
to a list, I should do something like:

for file in list_of_backup_files:
some_list.append(file)
By the way I did this:

def listAllbackups(filename):
list_of_backups = glob(home+'/Desktop/backupdir/*%s*'%filename)
for element in list_of_back:
if element.find(file) != -1:
date_components = element.split('-')[-4:-1]
date = str(date_components[0]) + ":" + str(date_components[1]) +
":" + str(date_components[2])
time = element.split('-')[-1]
yield (date, time)
print listAllbackups('fstab')


I ran it in the terminal and I got:

<generator object at 0x81ed58c>

Why does it do that and not just print out all the values of (date,
time)
Regards,
Shriphani Palakodety
 
S

Steve Holden

Shriphani said:
Hello,
Would that mean that if I wanted to append all the (date, time) tuples
to a list, I should do something like:

for file in list_of_backup_files:
some_list.append(file)

That would be one way to do it (assuming you started with some_list as
an empty list). But a faster way would be to use a list comprehension
and say

some_list = [f for f in list_of_backup_files]
By the way I did this:

def listAllbackups(filename):
list_of_backups = glob(home+'/Desktop/backupdir/*%s*'%filename)
for element in list_of_back:
if element.find(file) != -1:
date_components = element.split('-')[-4:-1]
date = str(date_components[0]) + ":" + str(date_components[1]) +
":" + str(date_components[2])
time = element.split('-')[-1]
yield (date, time)
print listAllbackups('fstab')


I ran it in the terminal and I got:

<generator object at 0x81ed58c>

Why does it do that and not just print out all the values of (date,
time)

Because the generator object returned by the function has to be used in
an iterative context to produce its values. Which is why the list
comprehension produces a list!

If you don't really want a generator then you could just rewrite the
function to return a list in the first place. You did specifically ask
"how do I use the yield statement ...". If you are going to iterate o
ver the list then a generator is typically more memory-efficient
(because it only produces one element at a time), and can do things that
a list can't (like deal with a potentially infinite sequence of results).

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden

Sorry, the dog ate my .sigline
 
M

Marc 'BlackJack' Rintsch

Shriphani said:
Hello,
Would that mean that if I wanted to append all the (date, time) tuples
to a list, I should do something like:

for file in list_of_backup_files:
some_list.append(file)

That would be one way to do it (assuming you started with some_list as
an empty list). But a faster way would be to use a list comprehension
and say

some_list = [f for f in list_of_backup_files]

Or:

some_list = list(list_of_backup_files)

If `some_list` is not empty:

some_list.extend(list_of_backup_files)

Ciao,
Marc 'BlackJack' Rintsch
 
G

Gabriel Genellina

def listAllbackups(filename):
list_of_backups = glob(home+'/Desktop/backupdir/*%s*'%filename)
for element in list_of_back:
if element.find(file) != -1:
date_components = element.split('-')[-4:-1]
date = str(date_components[0]) + ":" + str(date_components[1]) +
":" + str(date_components[2])
time = element.split('-')[-1]
yield (date, time)
print listAllbackups('fstab')


I ran it in the terminal and I got:

<generator object at 0x81ed58c>

Why does it do that and not just print out all the values of (date,
time)

Because listAllbackups is a generator function: each time someone asks it
the next value, it runs until the "yield" statement, and pauses. Next time
execution continues from the same point.
You have to iterate over it. Try:

for date,time in listAllbackups('fstab'):
print date,time

or

items = list(listAllbackups('fstab'))
print items
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top