python - What is causing this error and how can I fix it? -
i trying make simple compression program in python, receiving error
with open("admin.dll", "r").read() text: attributeerror: __exit__
why getting error? full code
import zlib, sys, time, base64 open("admin.txt", "r").read() file: print("uncompressed: " + str(sys.getsizeof(file))) compressed = zlib.compress(file, 9) print("compressed: ", end="") print(sys.getsizeof(compressed))
you asking python treat result of expression open("admin.dll", "r").read()
(a string) context manager. context managers expected have __exit__
method, strings don't have methods.
you'd pass in file object:
with open("admin.dll", "r") fileobj: text = fileobj.read()
file objects do have required context manager methods.
note have other errors too; sys.getsizeof
produces memory size of python object, not size of file. use os.stat()
that, or seek end of file , use fileobj.tell()
size. size of compressed result, use len()
.
Comments
Post a Comment