First of all, Jeremy already pointed out that your code has several errors.
I'll assume though that your real code is more complicated and you just
tried to show us a simplification of your code.
On the other hand, without the real code we cannot tell why it works for 1
level and it does not work for 2 levels. As someone pointed out already, it
migh be because you are using "\" instead of "\\" for path separators.
Python will accept "\c" as if it was with double backslashes but it will
reject "\x" because that is a special character.
Based on the example that you gave, you are also misunderstanding os.walk.
os.walk returns 3 values (I'll use the names in your code):
- root - the parent directory at this point in the traversal of the tree
- dir - the list of children directories in root (right below root)
- files - the list of files in root
So the files in the 'files' list are not under dir but under root. So it
does not make sense to loop over 'files' inside the loop over 'dir' or to
join root+i+j. Both your 'i' and your 'j' are just under 'root'.
I'm not sure either what you are trying to do with "root+i+j". You probably
need os.path.join(root,i) and os.path.join(root,j)
Dan
"Anand K Rayudu" <ar*@esi-group.com> wrote in message
news:ma**************************************@pyth on.org...
Hi all,
I am trying to find a way to get the files recursively in a given
directory,
The following code is failing, can some one please suggest what could be
problem here
from os import walk,join
for root,dir,files in os.walk("E:\myDir1\MyDir2"):
for i in dir:
for j in files:
fille = root+i+j
print file
Surprisingly if i give os.walk("E:\myDir1") the above code works, but not
if i have 2 levels of directories.
Thanks & Best Regards,
Anand