2

For whatever reason i cannot open or access the file in this subdirectory. I need to be able to open and read files within subdirectories of a zipped folder. Here is my code.

import zipfile
import os

for root, dirs, files in os.walk('Z:\\STAR'):
    for name in files:
        if '.zip' in name:
            try:
                zipt=zipfile.ZipFile(os.path.join(root,name),'r')
                dirlist=zipfile.ZipFile.namelist(zipt)
                for item in dirlist:
                    if 'Usb' in item:
                        input(item)
                        with zipt.open(item,'r') as f:
                            a=f.readlines()
                            input(a[0])
                    else:pass
            except Exception as e:
                print('passed trc file {}{} because of {}'.format(root,name,e))
        else:pass

This code currently gives me the error:

File "StarMe_tracer2.py", line 133, in tracer
    if 'er99' in line:
TypeError: a bytes-like object is required, not 'str'
Dharman
  • 30,962
  • 25
  • 85
  • 135
Syllogism
  • 145
  • 1
  • 1
  • 10
  • 2
    Please provide the full traceback for your error. – dspencer Apr 08 '20 at 16:20
  • By traceback do you mean the error that the terminal returned? It is in a try: block so the traceback would only give you my print statement. What other info do you need? – Syllogism Apr 08 '20 at 16:25
  • Remove the `try` block and show us the traceback after that. – blhsing Apr 08 '20 at 16:27
  • 1
    You could temporarily `raise e` from that `except` so that you can see where the exception originates. – dspencer Apr 08 '20 at 16:27
  • The Print statement that I am receiving is ```passed trc file "insert-filename-here".zip because of a bytes-like object is required, not 'str'``` – Syllogism Apr 08 '20 at 16:30
  • Full traceback without try block: ```Traceback (most recent call last): File "StarMe_tracer2.py", line 250, in main() File "StarMe_tracer2.py", line 243, in main todo() File "StarMe_tracer2.py", line 236, in todo trclooper('Z:\\STAR') File "StarMe_tracer2.py", line 211, in trclooper a=tracer(data,name,root) File "StarMe_tracer2.py", line 133, in tracer if 'er99' in line: TypeError: a bytes-like object is required, not 'str'``` – Syllogism Apr 08 '20 at 16:31
  • I can not explicitly post my file architecture but the issue is "I presume" the fact that the file in question is nested in a subfolder of this zipped directory – Syllogism Apr 08 '20 at 16:32

1 Answers1

2

The content read from the file object opened with ZipFile.open is bytes rather than a string, so testing if a string 'er99' is in a line of bytes would fail with a TypeError.

Instead, you can either decode the line before you test:

if 'er99' in line.decode():

or convert the bytes stream to a text stream with io.TextIOWrapper:

import io

...

with io.TextIOWrapper(zipt.open(item,'r'), encoding='utf-8') as f:
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • Each of the elements in the ```dirlist``` variable are ```str``` when I check thier ```type()``` will this help me open the file itself at all or just help in identifying the file in the ```os.walk()```? – Syllogism Apr 08 '20 at 16:38
  • if I change ```zipt.open(item)``` to ```zipt.open(item.filename)``` i still get the same error :( – Syllogism Apr 08 '20 at 16:39
  • 1
    Oops, fixed my answer according to the traceback in your comment. – blhsing Apr 08 '20 at 16:48