Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:text': 0.05; 'correct.': 0.07; 'bytes,': 0.09; 'bytes.': 0.09; 'encode': 0.09; 'filenames': 0.09; 'so?': 0.09; 'violates': 0.09; 'subject:question': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; 'codec': 0.16; 'example).': 0.16; 'fine.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'pathnames': 0.16; 'underlying': 0.16; 'wrote:': 0.18; 'have:': 0.19; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'fine': 0.24; 'regardless': 0.24; 'file.': 0.24; 'looks': 0.24; 'cc:2**0': 0.24; 'developers': 0.25; 'mention': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'character': 0.29; 'forgot': 0.30; 'message- id:@mail.gmail.com': 0.30; '"",': 0.31; 'file': 0.32; 'linux': 0.33; '(most': 0.33; "can't": 0.35; 'objects': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'version': 0.36; 'consistent': 0.36; 'should': 0.36; 'two': 0.37; 'that,': 0.38; 'recent': 0.39; 'itself': 0.39; 'skip:u 10': 0.60; 'skip:o 30': 0.61; 'here:': 0.62; 'refer': 0.63; 'stand': 0.64; 'mar': 0.68; '2015': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=LoJq+r2d7jFL7l8pu0DaZPSYTBIj9OeZ4ic76DIHk5Y=; b=tvd37VV1yahmHPSg9/NmAG4lWYELVkpPCk79TnS+7KeSJ+Kst1abMfaL4W6i2CG1X9 liijCKsTw2FwX5SdANIyKVGzjjklZ5yTwgoiagvvp4m7iImiVJz8O/6RZjgqkoWkGKGr fBW8EhabF1zzJzIr3n7H7ZXix1U8pn/prWeK6nloqrMycvYHax/k0ZxeSK0rnTRBkT7i kKWcJXq6AbPbYzLxHvrMWLvEYI5TVhjQNumZtgGC1mIPxNbbaDczPSE4j4iQ1emqYVO/ ds/ZYiJ89uUEzgnK3HNOTyQiM8BDKpqcUUjifM/2k0NUW3ttAtStZzYwE/hTromyzVP1 koZQ== MIME-Version: 1.0 X-Received: by 10.50.131.196 with SMTP id oo4mr36495103igb.2.1425751196919; Sat, 07 Mar 2015 09:59:56 -0800 (PST) In-Reply-To: <87y4n8wvc3.fsf@elektro.pacujo.net> References: <9169f3b1-2ac7-42a3-8033-584f84b88a1f@googlegroups.com> <7a75a23c-4678-4d7a-a2ec-9e8fff4c07f8@googlegroups.com> <132d5ce6-f672-4eec-99f9-1cc9e88b94f3@googlegroups.com> <619e4cb5-1c4c-449b-a5d7-951101b32b45@googlegroups.com> <54f862ca$0$13014$c3e8da3$5496439d@news.astraweb.com> <54fadc70$0$13004$c3e8da3$5496439d@news.astraweb.com> <87twxxxbvd.fsf@elektro.pacujo.net> <54fb1bf4$0$12993$c3e8da3$5496439d@news.astraweb.com> <87twxw4xlz.fsf@elektro.pacujo.net> <87k2ysydtk.fsf@elektro.pacujo.net> <87bnk4yci1.fsf@elektro.pacujo.net> <877fusybkb.fsf@elektro.pacujo.net> <87y4n8wvc3.fsf@elektro.pacujo.net> Date: Sun, 8 Mar 2015 04:59:56 +1100 Subject: Re: Newbie question about text encoding From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.19 Precedence: list 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: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1425751200 news.xs4all.nl 2901 [2001:888:2000:d::a6]:46001 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:87116 On Sun, Mar 8, 2015 at 4:50 AM, Marko Rauhamaa wrote: >> There are two things happening here: >> >> 1) The underlying file system is not UTF-8, and you can't depend on >> that, > > Correct. Linux pathnames are octet strings regardless of the locale. > > That's why Linux developers should refer to filenames using bytes. > Unfortunately, Python itself violates that principle by having > os.listdir() return str objects (to mention one example). Only because you gave it a str with the path name. If you want to refer to file names using bytes, then be consistent and refer to ALL file names using bytes. As I demonstrated, that works just fine. >> 2) You forgot to put the path on that, so it failed to find the file. >> Here's my version of your demo: >> >>>>> open("/tmp/xyz/"+os.listdir('/tmp/xyz')[0]) >> <_io.TextIOWrapper name='/tmp/xyz/\udc80' mode='r' encoding='UTF-8'> >> >> Looks fine to me. > > I stand corrected. > > Then we have: > > >>> os.listdir()[0].encode('utf-8') > Traceback (most recent call last): > File "", line 1, in > UnicodeEncodeError: 'utf-8' codec can't encode character '\udc80' in > position 0: surrogates not allowed So? ChrisA