0

I have a simple python script that takes a text file from a zip file with Zipfile and puts the text file into a variable. The issue I am having is that the output from the file inside the zip is being put into a single block of text and not being done by each line like the original file in the zip file is formatted. For example I am using a Cisco configuration file that has many lines and I would like to print the results out exactly as the file is formatted.


import zipfile

zip = zipfile.ZipFile('files.zip')

print (zip.namelist())
names = zip.namelist()
filename = names[0]
print (filename)

f = zip.open(filename)

print (f.readlines())

The output looks like this when I want it to be each line like the original configuration file;

[b'!\n', b'! Last configuration change at 17:10:20 UTC Tue Aug 4 2020\n', b'!\n', b'version 15.2\n', b'service timestamps debug datetime msec\n', b'service timestamps log datetime msec\n', b'no service password-encryption\n', b'service compress-config\n', b'!\n', b'hostname IOU3\n', b'!\n', b'boot-start-marker\n', b'boot-end-marker\n', b'!\n', b'!\n', b'logging discriminator EXCESS severity drops 6 msg-body drops EXCESSCOLL \n', b'logging buffered 50000\n', b'logging console discriminator EXCESS\n', b'!\n', b'no aaa new-model\n', b'!\n', b'!\n', b'!\n', b'!\n', b'!\n', b'no ip icmp rate-limit unreachable\n', b'!\n', b'!\n', b'!\n', b'no ip domain-lookup\n', b'ip cef\n', b'no ipv6 cef\n', b'!\n', b'!\n', b'!\n', b'spanning-tree mode rapid-pvst\n', b'spanning-tree extend system-id\n', b'!\n', b'vlan internal allocation policy ascending\n', b'!\n', b'ip tcp synwait-time 5\n', b'! \n', b'!\n', b'!\n'
Dharman
  • 30,962
  • 25
  • 85
  • 135
PSwill2
  • 23
  • 3
  • I don't understand how the output is different from "each line like the original configuration file". Do you understand what a `list` is? – Karl Knechtel Oct 18 '20 at 15:54
  • I tried to use spltlines() and I still get the same results. When I print the variable I would like it printed out exactly as it is formatted in the original text file. – PSwill2 Oct 18 '20 at 15:58

0 Answers0