New to Programming: TypeError: coercing to Unicode: need string or buffer, list found
rosuav at gmail.com
Thu Apr 2 14:24:54 CEST 2015
On Thu, Apr 2, 2015 at 11:02 PM, Saran A <ahlusar.ahluwalia at gmail.com> 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__":
... 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:
Here's another one, this time built for Windows:
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.
More information about the Python-list