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.
Any ideas here?
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?