lists and files question

Mark Day 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[1],'rb')
> 
> Produces this error:
> 
> IOError: Error[2] 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).

Try this:
   content = open(os.path.join(root,fname[1]), '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).

-Mark




More information about the Python-list mailing list