1

I have a python script which I am trying to setup to output a json directory listing to a webpage. I'm making progress, but now I've run against a wall. By the way, I have almost no python experience. Here is my code:

if self.path.endswith("/"):
    self.send_response(200)
    self.send_header('Content-type',    'text/plain')
    self.end_headers()
    dirlist = os.listdir
    self.wfile.write(dirlist)
    return

and here is my error:

  File "C:\Documents and Settings\Admin\Desktop\TiddlyWikis\Songs\Audio\server.p
y", line 18, in do_GET
    self.wfile.write(dirlist)
  File "C:\Python32\lib\socket.py", line 297, in write
    return self._sock.send(b)
TypeError: 'builtin_function_or_method' does not support the buffer interface

by the way, if you know of a python script that outputs json and everything, I'd be glad to just use it, since I'm nowhere near done with this.

4 Answers 4

0

os.listdir(path) Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order. It does not include the special entries '.' and '..' even if they are present in the directory.
http://docs.python.org/library/os.html#os.listdir

import json and

if self.path.endswith("/"):
    self.send_response(200)
    self.send_header('Content-type', 'application/json')
    self.end_headers()
    dirlist = os.listdir('.')   # Change the path here
    self.wfile.write(json.dumps(dirlist))
    return
3
  • I get this error: TypeError: 'str' does not support the buffer interface. What could possibly be the problem? Jan 27, 2012 at 15:34
  • Try this self.wfile.write(buffer(json.dumps(dirlist)))
    – Insidi0us
    Jan 27, 2012 at 17:13
  • @ArlenBeiler: try self.wfile.write(json.dumps(dirlist).encode('utf8')). Of course, you can use any other encoding instead of utf-8
    – avtomaton
    Jun 28, 2015 at 16:59
0

Firstly you forgot to call listdir:

dirlist = os.listdir

This assign the function listdir to the name dirlist. That's not what you want, you want to get a list of files in a directory. You get that by calling the function listdir:

dirlist = os.listdir(path)

where path is the path to the directory you want to list.

Secondly, you are trying to write Unicode strings to a binary output. Don't construct the json data yourself, use the json library for that. http://docs.python.org/dev/library/json.html

0

Well, I haven't the slightest idea what I was doing wrong, but I copied the list_directory function from http.py into my script and it works. Now I can work from here.

class MyHandler(http.server.SimpleHTTPRequestHandler):
    def list_directory(self, path):
        #list_directory function code

Hopefully what is posted can help others.

-1

Try converting to a string explicitly first:

self.wfile.write(str(dirlist))

Then, with JSON:

self.wfile.write(json.dumps(dirlist))

You just need to format the dirlist "buffer" so that write() understands how to send it.

2
  • using json.dumps I get TypeError: <built-in function listdir> is not JSON serializable. str says TypeError: 'str' does not support the buffer interface Jan 27, 2012 at 13:37
  • That's because you were doing dirlist = os.listdir instead of dirlist = os.listdir() as another answer pointed out. Jan 30, 2012 at 16:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.