Changing filenames from Greeklish => Greek (subprocess complain)

Cameron Simpson cs at zip.com.au
Sun Jun 9 05:16:06 EDT 2013


On 09Jun2013 08:15, Steven D'Aprano <steve+comp.lang.python at pearwood.info> wrote:
| On Sun, 09 Jun 2013 00:00:53 -0700, nagia.retsina wrote:
| > path = b'/home/nikos/public_html/data/apps/'
| > files = os.listdir( path )
| > 
| > for filename in files:
| > 	# Compute 'path/to/filename'
| > 	filepath_bytes = path + filename
| > 	for encoding in ('utf-8', 'iso-8859-7', 'latin-1'):
| > 		try:
| > 			filepath = filepath_bytes.decode( encoding )
| > 		except UnicodeDecodeError:
| > 			continue
| >         
| > 		# Rename to something valid in UTF-8
| > 		if encoding != 'utf-8':
| > 			os.rename( filepath_bytes, 
| >                                  filepath.encode('utf-8') )
| > 		assert os.path.exists( filepath )
| > 		break
| > 	else:
| > 		# This only runs if we never reached the break 
| >               raise ValueError(
| >                     'unable to clean filename %r' % filepath_bytes )
| 
| Editing the traceback to get rid of unnecessary noise from the logging:
| 
| Traceback (most recent call last):
|   File "/home/nikos/public_html/cgi-bin/files.py", line 83, in <module>
|   assert os.path.exists( filepath )
|   File "/usr/local/lib/python3.3/genericpath.py", line 18, in exists
|   os.stat(path)
| UnicodeEncodeError: 'ascii' codec can't encode characters in position 
| 34-37: ordinal not in range(128)
| 
| > Why am i still receing unicode decore errors? With the help of you guys
| > we have writen a prodecure just to avoid this kind of decoding issues
| > and rename all greek_byted_filenames to utf-8_byted.
| 
| That's a very good question. It works for me when I test it, so I cannot 
| explain why it fails for you.

If he's lucky the UnicodeEncodeError occurred while trying to print
an error message, printing a greek Unicode string in the error with
ASCII as the output encoding (default when not a tty IIRC).

Cheers,
-- 
Cameron Simpson <cs at zip.com.au>

I generally avoid temptation unless I can't resist it.  - Mae West



More information about the Python-list mailing list