lists and files question
mday at apple.com
Wed Jul 23 02:33:22 CEST 2003
In article <3F1DCF52.7040607 at hotmail.com>, hokiegal99
<hokiegal99 at hotmail.com> wrote:
> This code:
> import os, re, string
> setpath = raw_input("Enter the path: ")
> for root, dirs, files in os.walk(setpath):
> id = re.compile('Microsoft Excel Worksheet')
> fname = files
> # print fname
> content = open(fname,'rb')
> Produces this error:
> IOError: Error No such file or directory 'Name of File'
> The strange thing is that it correctly identifies the file that it says
> doesn't exist. Could someone explain why this is?
The problem is that file doesn't exist in the current working
directory; it's in another directory (stored in "root" in your code).
content = open(os.path.join(root,fname), 'rb')
> Also, is "files" a nested list? It looks like one, but I'm not entirely
> sure as I'm still relatively new to Python. Thanks!
It is a list of strings. Each string is the name of one of the files
in the directory (whose path is in "root" above).
More information about the Python-list