Changing filenames from Greeklish => Greek (subprocess complain)

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Jun 9 04:15:07 EDT 2013


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.

Please try this: log into the Linux server, and then start up a Python 
interactive session by entering:

python3.3

at the $ prompt. Then, at the >>> prompt, enter these lines of code. You 
can copy and paste them:


import os, sys
print(sys.version)
s = ('\N{GREEK SMALL LETTER ALPHA}\N{GREEK SMALL LETTER BETA}'
     '\N{GREEK SMALL LETTER GAMMA}\N{GREEK SMALL LETTER DELTA}'
     '\N{GREEK SMALL LETTER EPSILON}')
print(s)
filename = '/tmp/' + s
open(filename, 'w')
os.path.exists(filename)


Copy and paste the results back here please.



> Is it the assert that fail? Do we have some logic error someplace i dont
> see?

Please read the error message. Does it say AssertionError?

If it says AssertionError, then the assert has failed. If it says 
something else, the code failed before the assert can run.


-- 
Steven



More information about the Python-list mailing list