PyMel- Getting texture file from a material

So, my first inelegant-yet-functional PyMel script is done and working. The core of it is in this little snippet here, that digs through a mesh's inputs until it finds the texture node on the surface shader:

#Pass through the mesh's shape and return a texture name. 
import pymel as pm

def getTextureFile(geo):

    sg = []

    #Get the shading group from the selected mesh
    raw_sg = pm.listConnections(geo, type='shadingEngine')

    #Rapidly check for duplicates in the list- I do this by checking each item 
    #against an empty list. 
    #If the item is not in the empty list I append it. 
    for element in raw_sg:
        if element not in sg:

    #Get some info
    sgInfo = pm.listConnections(sg[0], type='materialInfo')
    fileNode = pm.listConnections(sgInfo[0], type='file')
    textureFile = pm.getAttr(fileNode[0].fileTextureName)

    print 'This is the file', str(textureFile)
    return str(textureFile)
It still leaves a lot to be desired, for instance it doesn't know what to do if a mesh doesn't actually have a texture! But, provided it does, using the win32com module I can then send the texture file information onto Photoshop and Alienbrain and open files/check out texture chains etc etc.

Thanks to Daydreamer who pointed out the missing pymel reference at the top of the snippet, as well as other issues with the script which I am working through...

After a happy weekend of actually reading part of the official tutorial, I have a slightly better understanding of how to access a mesh's attributes and nodes using PyMel. Here is a snippet of the above snippet that does pretty much the same thing, only using the pymel connections, input and output commands. The intention is only to test the functionality.
    import pymel as pm

    geo =[0].getShape()

    #Get the shading group from the selected mesh
    sg = geo.outputs(type='shadingEngine')

    #Work through the node to get the file texture name
    sgInfo = sg[0].connections(type='materialInfo')
    #It falls apart here if you have no file node! Oops...
    fileNode = sgInfo[0].connections(type='file')

    #Get the file texture name attribute's value
    textureFile = pm.getAttr(fileNode[0].fileTextureName)

    print 'This is the file', str(textureFile)