On Nov 15, 2007 2:15 PM, linda. s <[EMAIL PROTECTED]> wrote: > > On 11/15/07, Matimus <[EMAIL PROTECTED]> wrote: > > On Nov 15, 12:45 pm, linda.s <[EMAIL PROTECTED]> wrote: > > > I run the following code and got the error (I put a .gif file on the > > > desktop) > > > Traceback (most recent call last): > > > File "11.py", line 25, in <module> > > > for gifname in os.listdir(dirpath): > > > OSError: [Errno 2] No such file or directory: '.\\Desktop\\' > > > > > > import os > > > import Tkinter > > > > > > root = Tkinter.Tk() > > > L = Tkinter.Listbox(selectmode=Tkinter.SINGLE) > > > gifsdict = {} > > > > > > dirpath = '.\\Desktop\\' > > > for gifname in os.listdir(dirpath): > > > if not gifname[0].isdigit(): > > > continue > > > gifpath = os.path.join(dirpath, gifname) > > > gif = Tkinter.PhotoImage(file=gifpath) > > > gifsdict[gifname] = gif > > > L.insert(Tkinter.END, gifname) > > > > > > L.pack() > > > img = Tkinter.Label() > > > img.pack() > > > def list_entry_clicked(*ignore): > > > imgname = L.get(L.curselection()[0]) > > > img.config(image=gifsdict[imgname]) > > > L.bind('<ButtonRelease-1>', list_entry_clicked) > > > root.mainloop() > > > > The exception points to this line as being the issue: > > > for gifname in os.listdir(dirpath): > > > > and the error says `No such file or directory: '.\\Desktop\\'' > > > > So, there must be no directory named '.\\Desktop\\' in your current > > working directory. To find out your current working directory use > > `os.getcwd()'. Make sure that `os.getcwd()' returns the path to a > > directory with a Desktop folder in it. This is usually something like > > 'C:\\Documents and Settings\\username'. > > > > In my mac machine, > I got something like: > >>> os.getcwd() > '/Users/linda/Desktop' > > So I changed the code like: > dirpath = '.\Users\linda\Desktop\\' > > But I still got the error: > ValueError: invalid \x escape >
Two things. When you type out a path, if you include a '.' in the front, that means that the path is relative to your current working directory. So, if your current working directory is /Users/linda/Desktop, and dirpath is set to './Users/linda/desktop', then os.listdir(dirpath) is going to search '/Users/linda/desktop/Users/linda/Desktop', which probably doesn't exist. You want to use an absolute path just drop the '.'. If you want to get the files from the current directory, just use a single '.'. The following two things should work: dirpath = "." # This always uses the current working directory dirpath = "/Users/linda/Desktop" # This will always use the same directory The second thing, which I think is the error you are currently seeing, is the escape error. In strings in python the backslash is used as an escape character, if you want to type a single backslash you have to type two of them '\\' or, for paths (especially on a mac) is is completely valid to use forward slashes instead. Also, there are raw strings which don't do escaping. Any of the following will work: dirpath = "\\Users\\linda\\Desktop" dirpath = "/Users/linda/Desktop" dirpath = r"\Users\linda\Desktop" # prepending an 'r' makes it a raw string (no escaping) HTH Matt -- http://mail.python.org/mailman/listinfo/python-list