1

Specifically I want to know which properties are available on myFile in the following code sample:

def upload(self, myFile):
    out = """<html>
    <body>
        myFile length: %s<br />
        myFile filename: %s<br />
        myFile mime-type: %s
    </body>
    </html>"""

    # Although this just counts the file length, it demonstrates
    # how to read large files in chunks instead of all at once.
    # CherryPy reads the uploaded file into a temporary file;
    # myFile.file.read reads from that.
    size = 0
    while True:
        data = myFile.file.read(8192)
        if not data:
            break
        size += len(data)

    return out % (size, myFile.filename, myFile.content_type)
upload.exposed = True

This is taken from the CherryPy file upload example, and it shows a couple of the properties available in the documentation. Namely file, filename, content_type

But how can I determine all the properties, or better what the actual type is so I can open the source and read the properties?

Jim Wallace
  • 1,006
  • 8
  • 21
  • possible duplicate of [Python - Determine the type of an object?](http://stackoverflow.com/questions/2225038/python-determine-the-type-of-an-object) – loopbackbee May 28 '14 at 12:23
  • You could use `hasattr(object, name)` to see if name exist in object. Or simply try and catch a `AttributeError` exception. But maybe you could find an other way to be sure which properties you get (by having a stronger data model object for example) – Pierre Turpin May 28 '14 at 12:25

1 Answers1

2

The type can be obtained with type(myFile). You can use inspect module or myFile.__dict__ to see properties.

If you want to see the source code, use type(myFile).__module__ to see where it is defined.

Bartosz Marcinkowski
  • 6,651
  • 4
  • 39
  • 69