python 'function' object has no attribute 'GzipFile' -


i write function compress file following:

def gzip(filename):     '''gzip given file , remove original file.'''     r_file = open(filename, 'r')     w_file = gzip.gzipfile(filename + '.gz', 'w', 9)     w_file.write(r_file.read())     w_file.flush()     w_file.close()     r_file.close()     os.unlink(filename)  

however, when run program, got error:

'function' object has no attribute 'gzipfile'.

what did wrong? beforehand!

you've named function gzip same gzip module. now, when run function, python grabs function (think recursion) rather gzip module you've shadowed. there 2 solutions. 1) rename function:

def gzip_func():     ... 

2) give module different local name when importing:

import gzip gzip_mod ... def gzip():     ...     w_file = gzip_mod.gzipfile(filename + '.gz', 'w', 9) 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -