os.lisdir, gets unicode, returns unicode... USUALLY?!?!?

gabor gabor at nekomancer.net
Sun Nov 19 15:24:48 EST 2006


Fredrik Lundh wrote:
> gabor wrote:
> 
>> yes, sure... but then.. it's possible to implement it also on top of 
>> an raise-when-error version :)
> 
> not necessarily if raise-when-error means raise-error-in-os-listdir.
> 

could you please clarify?

currently i see 2 approaches how to do it on the raise-when-error version:

1.
dirname = u'something'
try:
	files = os.listdir(dirname)
except UnicodeError:
	byte_files = os.listdir(dirname.encode('encoding))
	#do something with it

2.

dirname = u'something'
byte_files = os.listdir(dirname.encode('encoding'))
for byte_file in byte_files:
	try:
		file = byte_file.decode(sys.getfsenc())
	except UnicodeError:
		#do something else
	#do something


the byte-string version of os.listdir remains. so all the other versions 
can be implemented on the top of it. imho the question is:
which should be the 'default' behavior, offered by the python standard 
library.

gabor



More information about the Python-list mailing list