breaking from loop
Ritesh Raj Sarraf
riteshsarraf at gmail.com
Fri Feb 10 09:01:44 EST 2006
Thanks to everyone. It is really the best place and the best people to
learn from.
Here's what I followed from the discussion:
def files(root):
for path, folders, files in os.walk(root):
for file in files:
yield path, file
def copy_first_match(repository, filename, dest_dir): # aka
walk_tree_copy()
for path, file in files(repository):
if file == filename:
try:
shutil.copy(os.path.join(path, file), dest_dir)
sys.stdout.write("%s copied from local cache %s." %
(file, repository))
except shutil.Error:
sys.stdout.write("%s is available in %s. Skipping
Copy!" % (file, dest_dir))
return True
return False
All I've added is the exception check because in case "file" is
available in "dest_dir", I want to display a message.
Since I'm still new and learning will be great if you let me know if
this is the proper way or not.
Thanks to everyone once again. It's been a very good experience
understanding everyone's comments.
Regards,
rrs
More information about the Python-list
mailing list