how to get files in a directory

Lonnie Princehouse finite.automaton at gmail.com
Wed Sep 29 23:44:41 CEST 2004


Backslash begins an escape sequence in a string literal. 
You have a few options:

0. Use a forward slash (which will work in Python for DOS paths):
"E:/myDir1/myDir2""

1. Use the escape sequence for backslash: "E:\\myDir1\\myDir2"

2. Use a raw string: r"E:\myDir1\myDir2"

Further reference:
    http://www.python.org/doc/current/ref/strings.html
 
> Surprisingly if i give os.walk("E:\myDir1") the above code works, but 
> not if i have 2 levels of directories.

Yes, that is kind of surprising.  Also, your nested loop...

      for i in dir:
       for j in files:
           fille = root+i+j
            print file

... doesn't make any sense.  "dir" is a list of sub-directories in
root, while "files" is a list of non-directory files in root.  The
files in "files" are not in the subdirectories.



More information about the Python-list mailing list