Changing filenames from Greeklish => Greek (subprocess complain)

Νικόλαος Κούρας nikos.gr33k at gmail.com
Sat Jun 8 12:53:03 EDT 2013


Sorry for th delay guys, was busy with other thigns today and i am still reading your resposes, still ahvent rewad them all just Cameron's:

Here is what i have now following Cameron's advices:


#========================================================
# Collect filenames of the path directory as bytes
path = b'/home/nikos/public_html/data/apps/'
filenames_bytes = os.listdir( path )

for filename_bytes in filenames_bytes:
	try:
		filename = filename_bytes.decode('utf-8)
	except UnicodeDecodeError:
		# Since its not a utf8 bytestring then its for sure a greek bytestring

		# Prepare arguments for rename to happen
		utf8_filename = filename_bytes.encode('utf-8')
		greek_filename = filename_bytes.encode('iso-8859-7')
		
		utf8_path = path + utf8_filename
		greek_path = path + greek_filename
		
		# Rename current filename from greek bytes --> utf8 bytes
		os.rename( greek_path, utf8_path )
==========================================

I know this is wrong though.
Since filename_bytes is the current filename encoded as utf8 or greek-iso
then i cannot just *encode* what is already encoded by doing this:

utf8_filename = filename_bytes.encode('utf-8')
greek_filename = filename_bytes.encode('iso-8859-7')



More information about the Python-list mailing list