Changing filenames from Greeklish => Greek (subprocess complain)

nagia.retsina at gmail.com nagia.retsina at gmail.com
Fri Jun 7 02:35:33 EDT 2013


Τη Παρασκευή, 7 Ιουνίου 2013 4:25:40 π.μ. UTC+3, ο χρήστης Steven D'Aprano έγραψε:

> MRAB tells you to work with the bytes, because the filenames' bytes are 
> invalid decoded as UTF-8. If you fix the file names by renaming using a 
> terminal set to UTF-8, then they will be valid and you can forget about  
> working with bytes.

Yes, but but 'putty' seems to always forget when i tell it to use utf8 for displaying and always picks up the Win8's default charset and it doesnt have a save options dialog. I cant always remember to switch to utf8 charset or renaming all the time from termnal so many greek filenames.

> Working with bytes is only for when the file names are turned to garbage.  
> Your file names (some of them) are turned to garbage. Fix them, and then 
> use file names as strings.

Can't '~/data/apps/' is filled every day with more and more files which are uploaded via FileZilla client, which i think it behaves pretty much like putty, uploading filenames as greek-iso bytes.

So that garbage will happen every day due to 'Putty' & 'FileZilla' clients.

So files.py before doing their stuff must do the automatic conversions from greek bytes to utf-8 bytes.

Here is what i have up until now.

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

# Iterate over all filenames in the path dir
for filename in filenames_bytes:
	# Compute 'path/to/filename' in bytes
	filepath_bytes = b'/home/nikos/public_html/data/apps/' + b'filename'
	try:
		filepath = filepath_bytes.decode('utf-8')
	except UnicodeDecodeError:
		try:
			filepath = filepath_bytes.decode('iso-8859-7')
			
			# Rename filename from greek bytes => utf-8 bytes
			os.rename( filepath_bytes filepath.encode('utf-8') )
		except UnicodeDecodeError:
			print "I give up! This filename is unreadable!"
=========================================

This is the best i can come up with, but after:

nikos at superhost.gr [~/www/cgi-bin]# python files.py
  File "files.py", line 75
    os.rename( filepath_bytes filepath.encode('utf-8') )
                                     ^
SyntaxError: invalid syntax
nikos at superhost.gr [~/www/cgi-bin]#
============================================


I am seeign the caret pointing at filepath but i cant follow what it tries to tell me. No parenthesis missed or added this time due to speed and tireness.

This rename statement tries to convert the greek byted filepath to utf-8 byted filepath.

I can't see whay this is wrong though.



More information about the Python-list mailing list