Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed2.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.013 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'encoding': 0.05; 'newbie': 0.05; 'subject:text': 0.05; 'encoded': 0.07; 'failing': 0.07; 'python3': 0.07; 'see:': 0.07; 'utf-8': 0.07; 'string': 0.09; 'bytes.': 0.09; 'way:': 0.09; 'subject:question': 0.10; 'cc:addr :python-list': 0.11; 'python': 0.11; 'mkdir': 0.16; 'underlying': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'byte': 0.24; 'unicode': 0.24; 'fine': 0.24; 'file.': 0.24; 'looks': 0.24; '---': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'handling': 0.26; 'pass': 0.26; 'header:In-Reply- To:1': 0.27; 'chris': 0.29; 'am,': 0.29; 'dec': 0.30; 'forgot': 0.30; 'that.': 0.31; '"",': 0.31; '>>>>': 0.31; 'though.': 0.31; 'file': 0.32; 'quite': 0.32; 'text': 0.33; 'linux': 0.33; '(most': 0.33; '-----': 0.33; 'problem': 0.35; "can't": 0.35; 'there': 0.35; 'version': 0.36; 'two': 0.37; 'list': 0.37; 'skip:o 20': 0.38; 'hat': 0.38; 'that,': 0.38; 'recent': 0.39; 'subject:': 0.39; 'how': 0.40; 'even': 0.60; 'received:67.195': 0.60; 'received:98.137': 0.60; 'skip:o 30': 0.61; 'march': 0.61; "you're": 0.61; 'here:': 0.62; 'back': 0.62; 're:': 0.63; 'such': 0.63; 'more': 0.64; 'to:addr:gmail.com': 0.65; 'header:Reply- To:1': 0.67; 'mar': 0.68; 'email addr:python.org"': 0.68; 'received:gq1.yahoo.com': 0.68; 'received:mail.gq1.yahoo.com': 0.68; 'touch': 0.74; 'special': 0.74; '2014,': 0.84; '2015': 0.84; 'directory:': 0.84; 'nice,': 0.84; 'received:bullet.mail.gq1.yahoo.com': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1425755186; bh=vwSszoTVWSqfNdbMcFeBbxepOKTlf2fj8JHmFUTP6qE=; h=Date:From:Reply-To:To:Cc:In-Reply-To:References:Subject:From:Subject; b=G7/LU4fRHFLgBtrEpgQazuMPd0U6D5Wc9nNW97+ZMUvSehmHVxpIVXMvXWvCw15lb6+8meH1n7ztg13XC5DSKMKNvsz9TxU3oD/Int1kJIZKsCHXfYne10cO5ffI7t69fbPaczUwE9bU3UtdXrQT7teQmMjrvAcQMArXNYsX3SbkhwYHKvccLxbjysfwR1a3LUGhU/LZh6R4pbSeh1lEwkiE/0oDH2Qn6ijl9qWyxG5wolreDxv5++DNO+lQg6XtA2O0wrwP7QicYWZoNFl4wPYYv0kf422VYOcLExZ849p7EBfHJqdtcgdLRuLyQtZJKD56feZ2iMhuCnUvINc3Zg== X-Yahoo-Newman-Property: ymail-4 X-Yahoo-Newman-Id: 220293.41900.bm@omp1028.mail.gq1.yahoo.com X-YMail-OSG: 7Pf3DMwVM1kiADEOALFVW8_qwlDRnpYJi7EaiUmelmrXsuR60J_ws0l1Knlpwfk- Date: Sat, 7 Mar 2015 19:03:34 +0000 (UTC) From: Albert-Jan Roskam Reply-To: Albert-Jan Roskam To: Chris Angelico Cc: "python-list@python.org" In-Reply-To: References: Subject: Re: Newbie question about text encoding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit 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: 56 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1425755314 news.xs4all.nl 2951 [2001:888:2000:d::a6]:35011 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:87126 --- Original Message ----- > From: Chris Angelico > To: > Cc: "python-list@python.org" > Sent: Saturday, March 7, 2015 6:26 PM > Subject: Re: Newbie question about text encoding > > On Sun, Mar 8, 2015 at 4:14 AM, Marko Rauhamaa wrote: >> See: >> >> $ mkdir /tmp/xyz >> $ touch /tmp/xyz/ >> \x80' >> $ python3 >> Python 3.3.2 (default, Dec 4 2014, 12:49:00) >> [GCC 4.8.3 20140911 (Red Hat 4.8.3-7)] on linux >> Type "help", "copyright", "credits" or > "license" for more information. >> >>> import os >> >>> os.listdir('/tmp/xyz') >> ['\udc80'] >> >>> open(os.listdir('/tmp/xyz')[0]) >> Traceback (most recent call last): >> File "", line 1, in >> FileNotFoundError: [Errno 2] No such file or directory: > '\udc80' >> >> File names encoded with Latin-X are quite commonplace even in UTF-8 >> locales. > > That is not a problem with UTF-8, though. I don't understand how > you're blaming UTF-8 for that. There are two things happening here: > > 1) The underlying file system is not UTF-8, and you can't depend on > that, ergo the decode to Unicode has to have some special handling of > failing bytes. > 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. > > Alternatively, if you pass a byte string to os.listdir, you get back a > list of byte string file names: > >>>> os.listdir(b"/tmp/xyz") > [b'\x80'] Nice, I did not know that. And glob.glob works the same way: it returns a list of ustrings when given a ustring, and returns bstrings when given a bstring.