Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed3.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'differently': 0.07; '"__main__":': 0.09; '__name__': 0.09; 'main()': 0.09; 'none)': 0.09; 'subject:string': 0.09; 'url:github': 0.09; 'cc:addr:python- list': 0.11; 'python': 0.11; 'def': 0.12; 'suggest': 0.14; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'instead).': 0.16; 'os.walk': 0.16; 'pythonic': 0.16; 'simple.': 0.16; 'subject: \n ': 0.16; 'subject:Programming': 0.16; 'subject:Unicode': 0.16; 'true:': 0.16; 'url:py': 0.16; 'windows:': 0.16; 'exception': 0.16; 'files.': 0.16; 'do,': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'bit': 0.19; 'trying': 0.19; 'subject:need': 0.19; 'thu,': 0.19; 'cc:addr:python.org': 0.22; 'print': 0.22; 'error': 0.23; 'instead.': 0.24; 'string,': 0.24; 'cc:2**0': 0.24; 'skip:" 20': 0.27; 'header:In-Reply-To:1': 0.27; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'getting': 0.31; 'file': 0.32; 'probably': 0.32; 'run': 0.32; 'another': 0.32; 'message.': 0.35; 'problem': 0.35; 'convert': 0.35; 'one,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'curious': 0.36; 'subject:New': 0.37; 'list': 0.37; 'skip:o 20': 0.38; 'pm,': 0.38; 'rather': 0.38; 'that,': 0.38; 'even': 0.60; 'skip:u 10': 0.60; 'then,': 0.60; 'most': 0.60; 'tell': 0.60; 'full': 0.61; 'new': 0.61; "you're": 0.61; "you'll": 0.62; 'sample': 0.67; 'between': 0.67; '(probably': 0.84; '2.7.': 0.84; '2015': 0.84; 'suspicion': 0.84; 'url:master': 0.84; 'absolutely': 0.87; 'to:none': 0.92; 'subject:found': 0.93 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=D5Mrw1itgZxoHzqWIrfJAPOkGs2ch+JDh4P9pd9C4OU=; b=iDCLcr0Mm5mdX+BO4sa31Kixvm4ojeFfYdrdNMSHg9TQAYpiWdZrfEi8lkRMZrg/Rx A4VSaq6gyn2sX3+Su4geE2Y6S4tUUsFB9ZMO5tHBKatnjQVmfqTgeVdXpnx7ln05crXa J65gQjguQwTDNb08vlWodtF00tFX4VuUZkmHGXeIM21a2na6aKO6wzL+xGOgwpeAkAUT P7r0W4a4NhufpgAOTrUT601jX0mlWux/cXtuV13VaaVsMrKiNA6kq5uRm/jKWRBqelmZ u1U5Qh7865k30UtuL6t9Sff9snFkosBBUAXQP3ffCeqOQHtRMEgtkozBM9hsoS5VEFjn r1BA== MIME-Version: 1.0 X-Received: by 10.43.38.144 with SMTP id ti16mr79938555icb.26.1427977494229; Thu, 02 Apr 2015 05:24:54 -0700 (PDT) In-Reply-To: <6203299c-f9b2-4169-9d68-4c92e0f7b32f@googlegroups.com> References: <6203299c-f9b2-4169-9d68-4c92e0f7b32f@googlegroups.com> Date: Thu, 2 Apr 2015 23:24:54 +1100 Subject: Re: New to Programming: TypeError: coercing to Unicode: need string or buffer, list found 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.20 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: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1427977502 news.xs4all.nl 2881 [2001:888:2000:d::a6]:51958 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:88442 On Thu, Apr 2, 2015 at 11:02 PM, Saran A wrote: > 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. > I don't think you actually want to convert a list into a string, here. Tell me if I'm understanding your code's intention correctly: > The sample code that I am trying to run is: > > path = "/Users/Desktop/Projects/" > dirlist = os.listdir(path) > before = dict([(f, None) for f in os.listdir(dirlist)]) Start up and get a full list of pre-existing files. > def main(dirlist): > while True: > time.sleep(10) #time between update check Then, every ten seconds... > after = dict([(f, None) for f in os.listdir(dirlist)]) > added = [f for f in after if not f in before] ... get a list of files, and if there are new ones... > if added: > print('Successfully added new file - ready to validate') > if __name__ == "__main__": > main() ... print out a message. If that's what you're trying to do, I would suggest using a directory notification system instead. Here's one that I use on Linux: https://github.com/Rosuav/shed/blob/master/dirwatch.py Here's another one, this time built for Windows: https://github.com/Rosuav/shed/blob/master/senddir.py But even if you absolutely have to poll, like that, you'll need to make a few code changes. The exception you're getting is symptomatic of just one problem with the code as published. My suspicion is that you just want to use listdir(path) rather than listdir(dirlist) - but if you want subdirectories, then you'll need to do things a bit differently (probably using os.walk instead). Also: You say you're using Python 2.7. If you have no particular reason to use 2.7, you'll do better to jump to Python 3. Your code will probably run identically, when it's this simple. ChrisA