In the code below I have a class that handles what folder the user is in, so they can browse through multiple folders. I know that this isn't going to work right just yet.

This is the first time I've tried using classes and I don't understand them so well. So I probably mucked something up somewhere.

This code seems like it's going to work, but I get an odd error when it tries to print the contents of the folder the user types.

class GetFoldersInDir:
    def DirName(self,directory):
        os.system("cls")
        self.directory = directory
        print "Folders in %s\n" % directory
        for folder in os.listdir(directory):
            print folder
        return self.directory

def delete():

    Get = GetFoldersInDir()
    Get.DirName("R:/")
    choice = raw_input("\nType del to delete something, type a folder's name to open it.\n")

    if choice == "del":
        print "@@@@@@"
    elif choice in os.listdir(Get.directory):
        Get.DirName(choice)
    else:
        print "Folder not found."
        print Get.directory

WindowsError: [Error 3] The system cannot find the path specified: 'Old/*.*'
Why does it add /*.* to what I type? :?:

and wheres the code of that part where you type a folder name to see it's contents.

Something to this direction, if I understood your intention?

import os
class Folder:
    def __init__(self, path):
        if os.path.isdir(path):
            self.path=os.path.realpath(path)
        else:
            raise WindowsError, 'Invalid path name: '+path
        
    def __str__(self):
        os.system("cls")
        res = "Folders in %s\n" % self.path
        return res+'\n\t'.join( filter(os.path.isdir,
                                       [os.path.join(self.path,p)
                                        for p in os.listdir(self.path)]))
    
    def cd(self,directory):
        curdir = os.path.join(self.path, directory)
        if os.path.isdir(curdir):
            self.path= os.path.realpath(curdir)
            return curdir
        else:
            return False
        
def delete():
    print """
    Type del to delete something,
    type a folder's name to open it,
    enter empty line to quit.
    """
    while True:
        print('-'*40)
        print myfolder
        choice = raw_input('>')

        if not choice:
            return
        elif choice == "del":
            print "dummy del",myfolder.path
        elif not myfolder.cd(choice):
            print "Folder not found."

myfolder = Folder("D:/")
delete()

YES! That is exactly what I was trying to do. Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.