Changing filenames from Greeklish => Greek (subprocess complain)

MRAB python at mrabarnett.plus.com
Thu Jun 6 08:50:52 EDT 2013


On 06/06/2013 13:04, Νικόλαος Κούρας wrote:
> First of all thank you for helping me MRAB.
> After make some alternation to your code ia have this:
>
> ----------------------------------------
> # Give the path as a bytestring so that we'll get the filenames as bytestrings
> path = b"/home/nikos/public_html/data/apps/"
>
> # Setting TESTING to True will make it print out what renamings it will do, but not actually do them
> TESTING = True
>
> # Walk through the files.
> for root, dirs, files in os.walk( path ):
> 	for filename in files:
> 		try:
> 			# Is this name encoded in UTF-8?
> 			filename.decode('utf-8')
> 		except UnicodeDecodeError:
> 			# Decoding from UTF-8 failed, which means that the name is not valid UTF-8
> 			# It appears that the filenames are encoded in ISO-8859-7, so decode from that and re-encode to UTF-8
> 			new_filename = filename.decode('iso-8859-7').encode('utf-8')
>
> 			old_path = os.path.join(root, filename)
> 			new_path = os.path.join(root, new_filename)
> 			if TESTING:
> 				print( '''<br>Will rename {!r} ---> {!r}<br><br>'''.format( old_path, new_path ) )
> 			else:
> 				print( '''<br>Renaming {!r} ---> {!r}<br><br>'''.format( old_path, new_path ) )
> 				os.rename( old_path, new_path )
> sys.exit(0)
> -------------------------
>
> and the output can be seen here: http://superhost.gr/cgi-bin/files.py
>
> We are in test mode so i dont know if when renaming actually take place what the encodings will be.
>
> Shall i switch off test mode and try it for real?
>
The first one is '/home/nikos/public_html/data/apps/Ευχή του Ιησού.mp3'.

The second one is '/home/nikos/public_html/data/apps/Σκέψου έναν 
αριθμό.exe'.

These names are currently encoded in ISO-8859-7, but will be encoded in
UTF-8 if you turn off test mode.

If you're happy for that change to happen, then go ahead.



More information about the Python-list mailing list