Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #88443

Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'string': 0.09; 'assuming': 0.09; 'none)': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:string': 0.09; 'python': 0.11; '"list': 0.16; 'buffer,': 0.16; 'loop.': 0.16; 'message;': 0.16; 'pythonic': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:Programming': 0.16; 'subject:Unicode': 0.16; 'terminate.': 0.16; 'true:': 0.16; 'typeerror:': 0.16; 'folder': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'trying': 0.19; 'subject:need': 0.19; 'bonus': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'sorry,': 0.24; 'string,': 0.24; 'decide': 0.24; 'looks': 0.24; 'this:': 0.26; 'pass': 0.26; 'skip:" 20': 0.27; 'header:X-Complaints-To:1': 0.27; 'idea': 0.28; 'point': 0.28; 'subject:list': 0.30; 'code': 0.31; 'directory,': 0.31; 'invoke': 0.31; 'run': 0.32; '(most': 0.33; 'actual': 0.34; 'sense': 0.34; 'problem': 0.35; 'convert': 0.35; 'curious': 0.36; 'possible': 0.36; 'subject:New': 0.37; 'list': 0.37; 'skip:o 20': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'is.': 0.60; 'most': 0.60; 'times': 0.62; 'such': 0.63; 'details': 0.65; 'sample': 0.67; 'between': 0.67; 'below:': 0.68; '2.7.': 0.84; 'morning:': 0.84; 'subject:found': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found
Date Thu, 02 Apr 2015 14:26:21 +0200
Organization None
References <6203299c-f9b2-4169-9d68-4c92e0f7b32f@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bd9d6d.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.19.1427977593.12925.python-list@python.org> (permalink)
Lines 48
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1427977593 news.xs4all.nl 2860 [2001:888:2000:d::a6]:53879
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:88443

Show key headers only | View raw


Saran A wrote:

> Good Morning:
> 
> I understand this error message when I run this code. However, I am
> curious to know what the most pythonic way is to convert  the list to a
> string? I use Python 2.7.
> 
> "Traceback (most recent call last):
> before = dict([(f, None) for f in os.listdir(dirlist)])
> TypeError: coercing to Unicode: need string or buffer, list found"
> 
> 
> The sample code that I am trying to run is:
> 
> path = "/Users/Desktop/Projects/"
> dirlist = os.listdir(path)

At this point dirlist is a list of names of the files and directories in 

"/Users/Desktop/Projects/"

Assuming that the Projects folder contains the subfolders or files
/Users/Desktop/Projects/foo, /Users/Desktop/Projects/bar and 
/Users/Desktop/Projects/baz dirlist looks like this:

["foo", "bar", "baz"]

It makes no sense to pass this list to os.listdir() as you do below:

> before = dict([(f, None) for f in os.listdir(dirlist)])

Forget about the other details in the error message; the actual problem is 
the "list found" part.

Now what would be a possible fix? Sorry, I have no idea what your intention 
is. Again, you don't need to convert your list to string, you need to decide 
what directory you want to pass to listdir(). If you have multiple such 
directories you need to invoke listdir() multiple times with a single 
directory, typically in a loop.

Bonus info:

>     while True:
>         time.sleep(10) #time between update check
 
This loop will never terminate.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Saran A <ahlusar.ahluwalia@gmail.com> - 2015-04-02 05:02 -0700
  Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Chris Angelico <rosuav@gmail.com> - 2015-04-02 23:24 +1100
    Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Saran A <ahlusar.ahluwalia@gmail.com> - 2015-04-02 05:46 -0700
      Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Chris Angelico <rosuav@gmail.com> - 2015-04-03 00:06 +1100
        Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Saran A <ahlusar.ahluwalia@gmail.com> - 2015-04-02 06:28 -0700
          Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Chris Angelico <rosuav@gmail.com> - 2015-04-03 00:57 +1100
      Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-04-02 20:03 -0400
        Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Saran A <ahlusar.ahluwalia@gmail.com> - 2015-04-02 17:14 -0700
          Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-04-03 11:33 -0400
      Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Chris Angelico <rosuav@gmail.com> - 2015-04-03 11:12 +1100
  Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Peter Otten <__peter__@web.de> - 2015-04-02 14:26 +0200
    Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found Saran A <ahlusar.ahluwalia@gmail.com> - 2015-04-02 05:51 -0700

csiph-web