os.path.join and lists

Steven D. Majewski sdm7g at Virginia.EDU
Wed Oct 17 15:48:23 EDT 2001


On Wed, 17 Oct 2001, Oleg Broytmann wrote:

> On Wed, Oct 17, 2001 at 07:01:20PM +0100, Dave Swegen wrote:
> > foo = os.path.join(bar[1:2], "bob")
> 
> foo = os.path.join(bar[1:2] + ["bob"]) # untested
> 

untested and unworking. It still ends up trying to concatenate
different types ( strings and lists ). All the args must be strings 
as there's a literal string in posixpath.py:
	path = path + '/' + b


Any of the following ought to work:

	# concat "bob" to the list and then join the list using pathsep:
	os.pathsep.join( bar[1:2] + ["bob"] )  

	# join only strings  slice  bar[1:2] == [ bar[1] ] 
	os.path.join( bar[1], "bob" )	

	# but if [1:2] could be any arbitrary slice, then apply
	# join to that list and then join "bob" separately:
	os.path.join( apply( os.path.join, bar[1:2] ), "bob" )


-- Steve Majewski






More information about the Python-list mailing list