Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Testing Zipfiles
#1
Over the holidays my main data drive crashed, requiring a deep data scan, which resulted in hundreds of thousands of files with extensions but no names.

I am trying to devise a utility to seperate out the .epub snd .jaava files (for now, more later), and it does appear to work BUT will crash even with exception handling (as I understand it) - if it comes across a 'BAD' zipfile.

Easiest solutioh at this point is to first seperate out ALL the zipfiles into GOOD and BAD categories.
The simple code below will work fine for GOOD files, but will halt after a BAD one is processes (moved).
Simply rerunning will continue the parsing until the next BAD. Ad infinitum.

I want it to continue to loop until the entire directory is parsed and eith ignore or move BAD zips.

#!python3

import os
import shutil
import zipfile
import glob
from zipfile import ZipFile
from zipfile import BadZipfile 


# location = 'c:/4/'
location = './'



for file in os.listdir(location):
   if file.endswith(".ZIP"):
       print(file)
       myfile = file
       try:
           with ZipFile(myfile, 'r') as zipObj:
               print("zipfile is OK")
       except BadZipfile:
           print("Does not work ")
           dirPath = 'BAD'
           if not os.path.isdir(dirPath): 
               print('The directory is not present. Creating a new one..')
               os.mkdir(dirPath)
           print("Myfile is", myfile ) 
           original = myfile
           target = 'BAD\\'
           shutil.move(original,target)
           os.remove(myfile)
           break
    
       print("Good Zip ")
       dirPath = 'GOOD'
       if not os.path.isdir(dirPath): 
           print('The directory is not present. Creating a new one..')
           os.mkdir(dirPath)
       print("Myfile is", myfile ) 
       original = myfile
       target = 'GOOD\\'
       ZipFile.close(zipObj)
       shutil.move(original,target)
Forgive the crudity of the code. I am still a n00b at python, and want to get the process working before I optimize

Any ideas here?
Reply


Messages In This Thread
Testing Zipfiles - by millpond - Jan-08-2021, 07:55 PM
RE: Testing Zipfiles - by buran - Jan-08-2021, 08:08 PM
RE: Testing Zipfiles - by millpond - Jan-09-2021, 09:11 AM
RE: Testing Zipfiles - by Gribouillis - Jan-09-2021, 09:18 AM
RE: Testing Zipfiles - by buran - Jan-09-2021, 09:26 AM
RE: Testing Zipfiles - by Gribouillis - Jan-09-2021, 10:00 AM
RE: Testing Zipfiles - by wavic - Jan-10-2021, 01:37 PM
RE: Testing Zipfiles - by millpond - Jan-11-2021, 10:44 PM

Forum Jump:

User Panel Messages

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