Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'charset:iso-8859-7': 0.04; 'syntax': 0.04; 'encoded': 0.07; 'reason,': 0.07; 'rename': 0.07; 'utf-8': 0.07; 'encode': 0.09; 'filename': 0.09; 'filenames': 0.09; 'statements': 0.09; 'try:': 0.09; 'python': 0.11; 'missed': 0.12; 'assume': 0.14; 'codec': 0.16; 'compute': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'simpson': 0.16; 'skip:" 80': 0.16; 'surrogate': 0.16; 'syntaxerror:': 0.16; 'traceback.': 0.16; 'utf8': 0.16; 'exception': 0.16; 'do,': 0.16; 'wrote:': 0.18; 'skip:f 30': 0.19; 'header:User-Agent:1': 0.23; 'error': 0.23; 'bytes': 0.24; 'errors.': 0.24; 'script': 0.25; 'handling': 0.26; 'query': 0.26; 'skip:" 40': 0.26; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'leave': 0.29; 'character': 0.29; 'statement': 0.30; 'code': 0.31; 'getting': 0.31; "skip:' 10": 0.31; 'raised': 0.31; 'skip:7 10': 0.31; 'skip:= 40': 0.31; 'skip:q 20': 0.31; 'file': 0.32; 'supposed': 0.32; 'skip:- 30': 0.32; 'says': 0.33; 'alone': 0.33; 'skip:b 30': 0.33; 'subject:from': 0.34; 'subject: (': 0.35; "can't": 0.35; 'except': 0.35; 'skip:u 20': 0.35; 'received:84': 0.35; 'but': 0.35; 'false': 0.36; 'up!': 0.36; 'next': 0.36; 'should': 0.36; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'skip:u 10': 0.60; 'first': 0.61; 'name': 0.63; 'by:': 0.65; 'god': 0.65; 'dont': 0.67; 'header:Reply-To:1': 0.67; 'invalid': 0.68; 'reply- to:no real name:2**0': 0.71; 'greek': 0.84; 'premature': 0.84; 'reply-to:addr:python.org': 0.84; 'sorry.': 0.91; '2013': 0.98 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=RZapVTdv c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=oyR3mlnJdzkA:10 a=AdgiQdVXbpoA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=E_FQspuQcigA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=uw23S90zXSUA:10 a=pGLkceISAAAA:8 a=S3oNkW3-bTbJvsh6Nk4A:9 a=o3X3QV6JOaEA:10 a=MSl-tDqOz04A:10 X-AUTH: mrabarnett:2500 Date: Sat, 08 Jun 2013 17:32:45 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130509 Thunderbird/17.0.6 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Changing filenames from Greeklish => Greek (subprocess complain) References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-7; format=flowed Content-Transfer-Encoding: 8bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 88 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1370709169 news.xs4all.nl 15937 [2001:888:2000:d::a6]:57220 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:47387 On 08/06/2013 07:49, Νικόλαος Κούρας wrote: > Τη Σάββατο, 8 Ιουνίου 2013 5:52:22 π.μ. UTC+3, ο χρήστης Cameron Simpson έγραψε: >> On 07Jun2013 11:52, =?utf-8?B?zp3Or866zr/PgiDOk866z4EzM866?= wrote: >> >> | nikos@superhost.gr [~/www/cgi-bin]# [Fri Jun 07 21:49:33 2013] [error] [client 79.103.41.173] File "/home/nikos/public_html/cgi-bin/files.py", line 81 >> >> | [Fri Jun 07 21:49:33 2013] [error] [client 79.103.41.173] if( flag == 'greek' ) >> >> | [Fri Jun 07 21:49:33 2013] [error] [client 79.103.41.173] ^ >> >> | [Fri Jun 07 21:49:33 2013] [error] [client 79.103.41.173] SyntaxError: invalid syntax >> >> | [Fri Jun 07 21:49:33 2013] [error] [client 79.103.41.173] Premature end of script headers: files.py >> >> | ------------------------------- >> >> | i dont know why that if statement errors. >> >> >> >> Python statements that continue (if, while, try etc) end in a colon, so: > > Oh iam very sorry. > Oh my God i cant beleive i missed a colon *again*: > > I have corrected this: > > #======================================================== > # Collect filenames of the path dir as bytes > filename_bytes = os.listdir( b'/home/nikos/public_html/data/apps/' ) > > for filename in filename_bytes: > # Compute 'path/to/filename' into bytes > filepath_bytes = b'/home/nikos/public_html/data/apps/' + b'filename' > flag = False > > try: > # Assume current file is utf8 encoded > filepath = filepath_bytes.decode('utf-8') > flag = 'utf8' > except UnicodeDecodeError: > try: > # Since current filename is not utf8 encoded then it has to be greek-iso encoded > filepath = filepath_bytes.decode('iso-8859-7') > flag = 'greek' > except UnicodeDecodeError: > print( '''I give up! File name is unreadable!''' ) > > if flag == 'greek': > # Rename filename from greek bytes --> utf-8 bytes > os.rename( filepath_bytes, filepath.encode('utf-8') ) > ================================== > > Now everythitng were supposed to work but instead iam getting this surrogate error once more. > What is this surrogate thing? > > Since i make use of error cathcing and handling like 'except UnicodeDecodeError:' > > then it utf8's decode fails for some reason, it should leave that file alone and try the next file? > try: > # Assume current file is utf8 encoded > filepath = filepath_bytes.decode('utf-8') > flag = 'utf8' > except UnicodeDecodeError: > > This is what it supposed to do, correct? > > ================================== > [Sat Jun 08 09:39:34 2013] [error] [client 79.103.41.173] File "/home/nikos/public_html/cgi-bin/files.py", line 94, in > [Sat Jun 08 09:39:34 2013] [error] [client 79.103.41.173] cur.execute('''SELECT url FROM files WHERE url = %s''', (filename,) ) > [Sat Jun 08 09:39:34 2013] [error] [client 79.103.41.173] File "/usr/local/lib/python3.3/site-packages/PyMySQL3-0.5-py3.3.egg/pymysql/cursors.py", line 108, in execute > [Sat Jun 08 09:39:34 2013] [error] [client 79.103.41.173] query = query.encode(charset) > [Sat Jun 08 09:39:34 2013] [error] [client 79.103.41.173] UnicodeEncodeError: 'utf-8' codec can't encode character '\\udcce' in position 35: surrogates not allowed > Look at the traceback. It says that the exception was raised by: query = query.encode(charset) which was called by: cur.execute('''SELECT url FROM files WHERE url = %s''', (filename,) ) But what is 'filename'? And what has it to do with the first code snippet? Does the traceback have _anything_ to do with the first code snippet?