Python Forum

Full Version: zip file variable prints <zipfile.ZipFile object at 0x7f83fd13bd90> ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Below are few lines on zip log file python script.
Getting output file name as different shown below,

zip_file_name = zipfile.ZipFile(server1 + '/' + log_file_name + ".zip", "w")
        
zip_file_name.write(server1, compress_type = zipfile.ZIP_DEFLATED)
print zip_file_name
output
Output:
<zipfile.ZipFile object at 0x7f83fd13bd90>
why its shows zip file name as mentioned above ? However am seeing log file got zipped !
The zipfile itself should be outside of "server1".
To zip the directory "server1", you have to loop over the files.

!/usr/bin/python3
import os, zipfile

server1 = "test"
log_file_name = "logfile"

zip_file = zipfile.ZipFile(log_file_name + ".zip", "w")
for dirname, subdirs, files in os.walk(server1):
    for filename in files:
        zip_file.write(os.path.join(dirname, filename))
zip_file.close()
The ZipFile object has no

https://docs.python.org/3/reference/data...t.__repr__ Wrote:object.__repr__(self)
  • Called by the repr() built-in function to compute the “official” string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form <...some useful description...> should be returned. The return value must be a string object. If a class defines __repr__() but not __str__(), then __repr__() is also used when an “informal” string representation of instances of that class is required.

    This is typically used for debugging, so it is important that the representation is information-rich and unambiguous.

or

https://docs.python.org/3/reference/data...ct.__str__ Wrote:object.__str__(self)
  • Called by str(object) and the built-in functions format() and print() to compute the “informal” or nicely printable string representation of an object. The return value must be a string object.

    This method differs from object.__repr__() in that there is no expectation that __str__() return a valid Python expression: a more convenient or concise representation can be used.

    The default implementation defined by the built-in type object calls object.__repr__().

class NewObect():
    pass

new_object = NewObect()

print(new_object)
Output:
<__main__.NewObect object at 0x0000021A05FD6710>
class NewObectWithRepr():
    def __repr__(self):
        return 'Has a string representation of the object'

new_object_with_repr = NewObectWithRepr()

print(new_object_with_repr)
Output:
Has a string representation of the object
import os
import zipfile


n1_4222_log = '/home/rsh/tibco_logs/emsn1_4222'

print('Checking dir: %s' % n1_4222_log)
dir_files = os.listdir(n1_4222_log)
archive_log = '/home/rsh/tibco_logs/emsn1_4222/archive'
print archive_log

for log_file_name in dir_files:

    if log_file_name == "archive":
        print(' Its Archive dir no need to zip %s' % log_file_name)
    else:
        print('Its log file %s' % log_file_name)
        zip_file_name = zipfile.ZipFile(n1_4222_log + '/' + log_file_name + ".zip", "w")
        print(zip_file_name)
        zip_file_name.write(n1_4222_log, compress_type = zipfile.ZIP_DEFLATED)
        print zip_file_name
[output]

Checking dir: /home/rsh/tibco_logs/emsn1_4222
/home/rsh/tibco_logs/emsn1_4222/archive
Its log file log
<zipfile.ZipFile object at 0x7f83fd13bd90>
<zipfile.ZipFile object at 0x7f83fd13bd90>
Its Archive dir archive
Its log file no need to zip log11
<zipfile.ZipFile object at 0x7f83fd13bdd0>
<zipfile.ZipFile object at 0x7f83fd13bdd0>
Its log file log12
<zipfile.ZipFile object at 0x7f83fd13bd90>
<zipfile.ZipFile object at 0x7f83fd13bd90>
Its log file log13
<zipfile.ZipFile object at 0x7f83fd13bdd0>
<zipfile.ZipFile object at 0x7f83fd13bdd0>
Its log file log14
<zipfile.ZipFile object at 0x7f83fd13bd90>
<zipfile.ZipFile object at 0x7f83fd13bd90>
Its log file log15
<zipfile.ZipFile object at 0x7f83fd13bdd0>
<zipfile.ZipFile object at 0x7f83fd13bdd0>
Its log file log11.zip
<zipfile.ZipFile object at 0x7f83fd13bd90>
<zipfile.ZipFile object at 0x7f83fd13bd90>

I am using python v2.7.5
in the dir, i can see this logs got zipped

-rw-rw-r-- 1 rsh rsh 976785 Dec 21 2018 log
drwxrwxr-x 2 rsh rsh 66 Jun 19 12:15 archive
-rw-rw-r-- 1 rsh rsh 976785 Jun 19 12:20 log15
-rw-rw-r-- 1 rsh rsh 976785 Jun 19 12:20 log14
-rw-rw-r-- 1 rsh rsh 976785 Jun 19 12:20 log13
-rw-rw-r-- 1 rsh rsh 976785 Jun 19 12:20 log12
-rw-rw-r-- 1 rsh rsh 976785 Jun 19 12:20 log11
-rw-rw-r-- 1 rsh rsh 182 Jun 24 09:05 log.zip
-rw-rw-r-- 1 rsh rsh 182 Jun 24 09:05 log15.zip
-rw-rw-r-- 1 rsh rsh 182 Jun 24 09:05 log14.zip
-rw-rw-r-- 1 rsh rsh 182 Jun 24 09:05 log13.zip
-rw-rw-r-- 1 rsh rsh 182 Jun 24 09:05 log12.zip
-rw-rw-r-- 1 rsh rsh 182 Jun 24 09:05 log11.zip

[output]

Error:
<zipfile.ZipFile object at 0x7f83fd13bd90>
As @Yoriz alread said, this is not an error:
Output:
<zipfile.ZipFile object at 0x7f83fd13bd90>
With Python3, you will get a better info:
Output:
<zipfile.ZipFile filename='logfile.zip' mode='w'>
If so, i need zip file need to be printed. what shld code there ?

If so, i need zip file name to be printed. what shld code there ?
(Jun-26-2019, 04:26 PM)Rsh Wrote: [ -> ]If so, i need zip file name to be printed. what shld code there ?
use/print ZipFile.filename property of the ZipFile object
Hi Buran,

tired print ZipFile.filename as shown below and got this below error
zip_file_name = zipfile.ZipFile(n1_4222_log + '/' + log_file_name + ".zip", "w")
        print ZipFile.zip_file_name
        zip_file_name.write(n1_4222_log, compress_type = zipfile.ZIP_DEFLATED)
Error:
Traceback (most recent call last): File "test_file.py", line 19, in <module> print ZipFile.zip_file_name NameError: name 'ZipFile' is not defined
print zip_file_name.filename
Finally we got it !
however it prints full path of zip file
am looking for zipfile name alone !

do we have any option in zipfile object ?

Checking dir: /home/rsh/tibco_logs/emsn1_4222
/home/rsh/tibco_logs/emsn1_4222/archive
Its log file log
/home/rsh/tibco_logs/emsn1_4222/log.zip
Its Archive dir archive
Its log file log11
/home/rsh/tibco_logs/emsn1_4222/log11.zip
Its log file log12
/home/rsh/tibco_logs/emsn1_4222/log12.zip
Its log file log13
/home/rsh/tibco_logs/emsn1_4222/log13.zip
Its log file log14
/home/rsh/tibco_logs/emsn1_4222/log14.zip
Its log file log15
/home/rsh/tibco_logs/emsn1_4222/log15.zip