N
N
NitroDesktop2021-04-21 11:43:32
Python
NitroDesktop, 2021-04-21 11:43:32

Python search for files in archives, how to fix?

I started with zip, but it seems I can’t understand at all what’s wrong, I even gave him time to think

. Error:


Traceback (most recent call last):
File "d:\Documents\test\scanner\sss.py", line 52, in
find_zip(sfile,text_file)
File "d:\Documents\test\scanner\sss.py", line 9, in find_zip
zipFile = zipfile.ZipFile(dir_file, 'r')
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\zipfile.py", line 1257, in __init__
self. _RealGetContents()
File "C:\Users\Admin\AppData\Local\Programs\Python\Python39\lib\zipfile.py", line 1324, in _RealGetContents
raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file

Here is what came out in the code:
import os
import zipfile
import time
def files(path):
    for file in os.listdir(path):
        if os.path.isfile(os.path.join(path, file)):
            yield file
def find_zip(dir_file,search):
    zipFile = zipfile.ZipFile(dir_file, 'r')
    info_arch=str(zipFile.namelist())
    info_arch=info_arch.replace(",", "")
    info_arch=info_arch.replace("'", "")
    info_arch=info_arch.replace("]", "")
    info_arch=info_arch.replace("[", "")
    # info_arch=info_arch.split()
    # print()
    count = 0
    f = open("scanarch.txt", "a")
    f.write("\n================================================================================================================\n")
    f.write("Archive: %s\n" % dir_file)
    for n,line in enumerate(info_arch.split()):
        if search in line:
            # sInfo="\n Line: "+str(n+1)+"\n "+"File: "+line
            sInfo="File: "+line
            count+=1
            f.write(sInfo+'\n')
            #  print(line,"Line: "+str(n+1)
            # print(sInfo)
    if count:
        f.write(u'\nFound Files: '+str(count))
    else:
        f.write("\nNot Found Files!")
    f.write("\n================================================================================================================")
    f.close
    # print(info_arch)
    zipFile.close()

filename=""
sfile=""
print("Введите диск где будем искать файлы")
dir_files=input()+":\\"
print("Что искать в архивах!")
text_file=input()
for top, dirs, files in os.walk(dir_files):
    for nm in files:       
        filename=os.path.join(top, nm)
        if filename!=sfile:
            sfile=filename
            ext = os.path.splitext(filename)[1]
            if ext=='.zip':
                time.sleep(3)
                find_zip(sfile,text_file)


If there is an opportunity for other archives, tell me, I don’t want to raise a lot of topics, thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
1
12rbah, 2021-04-21
@12rbah


raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file

Writes you File is not a zip file

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question