Ritesh Raj Sarraf wrote:
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
Ritesh Raj Sarraf enlightened us with:
bFound = True
break
return bFound
I see two weird things. First of all, the retun statement won't be
reached due to the break before it. Let's assume the break isn't
needed, and you
Ritesh Raj Sarraf wrote:
Following is the code:
def walk_tree_copy(sRepository, sFile, sSourceDir, bFound = None):
try:
if sRepository is not None:
for name in os.listdir(sRepository):
path = os.path.join(sRepository, name)
if
Ritesh Raj Sarraf wrote:
Hi,
Following is the code:
def walk_tree_copy(sRepository, sFile, sSourceDir, bFound = None):
try:
if sRepository is not None:
You're being overly defensive here. Passing None as first arg is clearly
a programming error, so the sooner you detect it
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,
Using the (non-standard yet) path module
(http://www.jorendorff.com/articles/python/path/), your code can be
simplified to:
from path import path, copy
def copy_first_match(repository, filename, dest_dir):
try:
first_match = path(repository).walkfiles(filename).next()
except
Hi,
Following is the code:
def walk_tree_copy(sRepository, sFile, sSourceDir, bFound = None):
try:
if sRepository is not None:
for name in os.listdir(sRepository):
path = os.path.join(sRepository, name)
if os.path.isdir(path):