Python Classes

Sam Collett sam_collett at lycos.co.uk
Thu Mar 7 05:26:03 EST 2002


I am quite new to Python and have no experience with classes. I have
tried a file class, but I am not sure how to get it to work:

class clFile(file):
    
    def getFileName(blnExt=None):
        """
        file = path to file
        blnExt = show file extension (put in value other than 0 or
None to show the extension)
        """
        import os
        if blnExt:
            result = os.path.basename(file)
        else:
            result = os.path.splitext(os.path.basename(file))[0]
        del os
        return result

    def getFileExt():
        import os.path
        result = os.path.splitext(file)[1]
        del os
        return result

    def getDir():
        import os
        result = os.path.dirname(file)
        del os
        return result

I wondered if there was a way to do something like the following:

myfile = clFile("c:\\somedir\\dir with spaces\\foo.txt")
myfile.getDir would return c:\somedir\dir with spaces\
myfile.getFileExt would return .txt
myfile.getFileName would return foo.txt or foo



More information about the Python-list mailing list