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 9 2236
On Tue, 2007-09-25 at 08:39 -0700, 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?
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.
--
Evan Klitzke <ev**@yelp.com>
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?
I would just do this: -
from glob import glob
-
def listallbackupfiles(filename):
-
return glob("/home/shriphani/backupdir/*%s*"%filename)
-
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
Larry Bates wrote:
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.
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
On Sep 25, 11:39 am, Shriphani <shripha...@gmail.comwrote:
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.
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
Shriphani wrote:
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
On Thu, 27 Sep 2007 08:16:59 -0400, Steve Holden wrote:
Shriphani wrote:
>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
En Thu, 27 Sep 2007 07:44:08 -0300, Shriphani <sh********@gmail.com>
escribi�:
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
--
Gabriel Genellina This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: inquirydog |
last post by:
Hi-
Does anyone know a way to compare whether two nodes contain
the same information in xslt (the name, attributes, and all content
recursivly should be the same. I am interested in the case...
|
by: Curtis Gilchrist |
last post by:
I am required to read in records from a file and store them in descending
order by an customer number, which is a c-style string of length 5. I am
storing these records in a linked list. My...
|
by: Odd-R. |
last post by:
I have to lists, A and B, that may, or may not be equal. If they are not
identical, I want the output to be three new lists, X,Y and Z where X has
all the elements that are in A, but not in B, and...
|
by: Sheldon |
last post by:
Hi,
I have two arrays that are identical and contain 1s and zeros. Only the
ones are valid and I need to know where both arrays have ones in the
same position. I thought logical_and would work...
|
by: jts2077 |
last post by:
I am trying to create a large nested XML object using E4X methods. The problem is the, the XML I am trying to create can only have xmlns set at the top 2 element levels. Such as:
<store
...
| |
by: JoeMac3313 |
last post by:
My Assignment was to compare two lists and print out the number of elments that are same.
It is supposed to look like this
Week 7 Homework
The number of common elements is: 3
The number of...
|
by: arnuld |
last post by:
It is quite an ugly hack but it is all I am able to come up with for
now :-( and it does the requires work. I want to improve the program, I
know you people have much better ideas ;-)
/* C++...
|
by: =?Utf-8?B?Sm9zaFA=?= |
last post by:
Hi All, I am attempting to compare values in two arraylists to make sure all
the values are the same. I am running into trouble with my code if both
arraylists compare okay up until a point and I...
|
by: Sean Dalton |
last post by:
Hello,
I have a two sets OLDLIST and REMOVE.
I would like to remove every element in OLDLIST if it is also occuring
in REMOVE
and store the remaining elements from OLDLIST into NEWLIST.
So...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |