Increment variable in callback during upload
Question: I have the following python script for an upload that needs to show percent
done. I am having trouble incrementing the variable that tracks the amount of
data transferred. I get an  
_UnboundLocalError: local variable 'intProgress' referenced before assignment_  
error. Yet if I try to print this variable it prints fine so it seems that it
is referenced.

    
    
    import os, sys, ftplib
    pathname = 'C:/Paradigm1/1.PNG'
    intFileSize = os.path.getsize(pathname)
    intPercentDone = 0
    intProgress = 0
    
    def callback(p):
        intProgress = intProgress + 1024
        ##sys.stdout.write(str(intProgress))
        sys.stdout.write("-")
    session = ftplib.FTP('Server','UserName','Password')
    f = open(pathname,'rb')# file to send
    session.storbinary('STOR /Ftp Accounts/PublicDownloads/test.png', f, 1024, callback)
    f.close()
    


Answer: If you want the `callback()` function to change the global variable
`intProgress`, you have to declare it as `global` in the function...

    
    
    def callback(p):
        global intProgress
        intProgress = intProgress + 1024
        ##sys.stdout.write(str(intProgress))
        sys.stdout.write("-")
    

...otherwise it'll assume `intProgress` is a local variable, and get confused
by the fact that you're trying to reference it when setting it.

