This function parses a full file specification into tuple of: a) list of drive and folders b) file name c) (last) file extension (including dot)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
def ParseFile( file ): import os (root, ext) = os.path.splitext(file) (x, name) = os.path.split(root) # dummy value y = '-' parts =  while y <> '': (x, y) = os.path.split(x) parts.append(y) parts = parts[:-1] if x: parts.append(x) parts.reverse() return (parts, name, ext) if __name__ == '__main__': # test code print ParseFile( 'c:/junk.py' ) print ParseFile( 'junk' ) print ParseFile( 'c:/test/junk.py.097' )
A full file specification consists of drive letter, folders, file name, and extension. Unless you're on UNIX. Unless there's more that one extension. Unless...
Sometimes it's nice to be able to decompose a file specification into its components in a standard way, taking into account all of these irregularities.
Note that (unlike some of the standard module functions) the file specification does not have to actually exist for ParseFile() to work.