Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.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.003 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'ideally': 0.04; 'encoding': 0.05; 'rename': 0.07; 'utf-8': 0.07; 'converted': 0.09; 'filename': 0.09; 'filenames': 0.09; 'try:': 0.09; 'worse': 0.09; 'assume': 0.14; '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; 'posted,': 0.16; 'utf8': 0.16; 'wrote:': 0.18; 'header:User- Agent:1': 0.23; 'byte': 0.24; 'bytes': 0.24; 'errors.': 0.24; 'header:In-Reply-To:1': 0.27; 'skip:g 30': 0.30; 'code': 0.31; "skip:' 10": 0.31; 'skip:b 30': 0.33; 'subject:from': 0.34; 'subject: (': 0.35; 'except': 0.35; 'sequence': 0.36; 'doing': 0.36; 'should': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'changed': 0.39; 'skip:u 10': 0.60; "you've": 0.63; 'more': 0.64; 'header:Reply-To:1': 0.67; 'reply- to:no real name:2**0': 0.71; '8bit%:100': 0.72; 'fails,': 0.84; 'greek': 0.84; 'reply-to:addr:python.org': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=KrN0hwmN 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=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=uw23S90zXSUA:10 a=91yAIpP8Tu03Nf28pIcA:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett:2500 Date: Fri, 07 Jun 2013 15:29:25 +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: <51B1842A.30104@gmail.com> <7d8da6c9-fb92-4329-b207-4280f29ba664@googlegroups.com> In-Reply-To: <7d8da6c9-fb92-4329-b207-4280f29ba664@googlegroups.com> Content-Type: text/plain; charset=UTF-8; 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: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1370615365 news.xs4all.nl 15998 [2001:888:2000:d::a6]:58184 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:47330 On 07/06/2013 12:53, Νικόλαος Κούρας wrote: [snip] > > #======================================================== > # Collect filenames of the path dir as bytes > greek_filenames = os.listdir( b'/home/nikos/public_html/data/apps/' ) > > for filename in greek_filenames: > # Compute 'path/to/filename' in bytes > greek_path = b'/home/nikos/public_html/data/apps/' + b'filename' > try: This is a worse way of doing it because the ISO-8859-7 encoding has 1 byte per codepoint, meaning that it's more 'tolerant' (if that's the word) of errors. A sequence of bytes that is actually UTF-8 can be decoded as ISO-8859-7, giving gibberish. UTF-8 is less tolerant, and it's the encoding that ideally you should be using everywhere, so it's better to assume UTF-8 and, if it fails, try ISO-8859-7 and then rename so that any names that were ISO-8859-7 will be converted to UTF-8. That's the reason I did it that way in the code I posted, but, yet again, you've changed it without understanding why! > filepath = greek_path.decode('iso-8859-7') > > # Rename current filename from greek bytes --> utf-8 bytes > os.rename( greek_path, filepath.encode('utf-8') ) > except UnicodeDecodeError: > # Since its not a greek bytestring then its a proper utf8 bytestring > filepath = greek_path.decode('utf-8') > [snip]