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

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

Android Gson.fromJson error -