Python Forum
zip file variable prints <zipfile.ZipFile object at 0x7f83fd13bd90> ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
zip file variable prints <zipfile.ZipFile object at 0x7f83fd13bd90> ?
#1
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 !
Reply
#2
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()
Reply
#3
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
Reply
#4
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>
Reply
#5
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'>
Reply
#6
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 ?
Reply
#7
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
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
Reply
#9
print zip_file_name.filename
Reply
#10
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  zipfile module error pseudo 3 693 Jun-30-2023, 03:52 PM
Last Post: Gribouillis
  zfill prints extra et the end of a var tester_V 4 848 Mar-24-2023, 06:59 PM
Last Post: tester_V
  [Solved by deanhystad] Create a zip file using zipfile library DZ_Galaxy 2 1,104 Aug-17-2022, 04:57 PM
Last Post: DZ_Galaxy
  Can ZipFile be used to extract hidden files? AiedailEclipsed 0 1,584 Mar-22-2022, 05:21 PM
Last Post: AiedailEclipsed
  variable prints without being declared. ClockPillow 2 1,770 Jul-11-2021, 12:13 AM
Last Post: ClockPillow
  Created zipfile without all the subfolder? korenron 3 3,605 Jun-23-2021, 12:44 PM
Last Post: korenron
  Create ZIP file not working using ZipFile? korenron 1 2,062 Jun-13-2021, 04:15 PM
Last Post: korenron
  Output prints Account.id at the end? LastStopDEVS 5 2,716 Dec-19-2020, 05:59 AM
Last Post: buran
  Defining an object's argument whose name is stored in a variable arbiel 2 2,144 Dec-11-2020, 10:19 PM
Last Post: arbiel
  When I print a key from dict it prints it but when I try to assign it to a variable I stefanvelikov 3 2,306 Nov-27-2020, 01:29 PM
Last Post: stefanvelikov

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020