Welcome, guest | Sign In | My Account | Store | Cart

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)

Python, 23 lines
 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.

2 comments

Daniel Kullmann 22 years, 7 months ago  # | flag

under UNIX... under UNIX, you don't need the "if x: parts.append(x)" part. the x var is empty, if the path name is not fully specified. if it is, the x var holds a "/", which should be omitted in the output list.

Robin Parmar (author) 22 years, 5 months ago  # | flag

but only UNIX. The lines in question are required for systems like Windows where there is a drive specification preceding the path.

Created by Robin Parmar on Tue, 10 Apr 2001 (PSF)
Python recipes (4591)
Robin Parmar's recipes (9)

Required Modules

Other Information and Tasks