Okey after reading also Steven post, i was relived form the previous suck 
position i was, so with an alternation of a few variable names here is the code 
now:


#========================================================
# Collect directory and its filenames as bytes
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 ) 

=================================

I dont know why it is still failing when it tried to decode stuff since it 
tries 3 ways of decoding. Here is the exact error.


ni...@superhost.gr [~/www/cgi-bin]# [Sat Jun 08 20:32:44 2013] [error] [client 
79.103.41.173] Error in sys.excepthook:
[Sat Jun 08 20:32:44 2013] [error] [client 79.103.41.173] ValueError: 
underlying buffer has been detached
[Sat Jun 08 20:32:44 2013] [error] [client 79.103.41.173]
[Sat Jun 08 20:32:44 2013] [error] [client 79.103.41.173] Original exception 
was:
[Sat Jun 08 20:32:44 2013] [error] [client 79.103.41.173] Traceback (most 
recent call last):
[Sat Jun 08 20:32:44 2013] [error] [client 79.103.41.173]   File 
"/home/nikos/public_html/cgi-bin/files.py", line 78, in <module>
[Sat Jun 08 20:32:44 2013] [error] [client 79.103.41.173]     assert 
os.path.exists( filepath )
[Sat Jun 08 20:32:44 2013] [error] [client 79.103.41.173]   File 
"/usr/local/lib/python3.3/genericpath.py", line 18, in exists
[Sat Jun 08 20:32:44 2013] [error] [client 79.103.41.173]     os.stat(path)
[Sat Jun 08 20:32:44 2013] [error] [client 79.103.41.173] UnicodeEncodeError: 
'ascii' codec can't encode characters in position 34-37: ordinal not in 
range(128)
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to