Skip to content

Instantly share code, notes, and snippets.

@mjdargen
Last active September 6, 2022 14:37
Show Gist options
  • Save mjdargen/f00bbdbaed8561aca69335c687f5c9b4 to your computer and use it in GitHub Desktop.
Save mjdargen/f00bbdbaed8561aca69335c687f5c9b4 to your computer and use it in GitHub Desktop.
import os
import shutil
from zipfile import ZipFile
def main():
# remove everything
for root, subdirs, _files in os.walk(os.getcwd()):
for d in subdirs:
shutil.rmtree(os.path.join(os.getcwd(), d))
for f in _files:
if 'zipit.py' != f and 'zipit.zip' != f:
os.remove(os.path.join(os.getcwd(), f))
dir = os.getcwd()
files = os.listdir(dir)
try:
filename = [f for f in files if f == 'zipit.zip'][0]
except IndexError:
print("Zip file not found or file not correctly named zipit.zip")
return 0
path = os.path.join(dir, filename)
with ZipFile(path, 'r') as zip:
zip.extractall(dir)
path = path[:-4]
# delete mac folders
for root, subdirs, files in os.walk(os.getcwd()):
for d in subdirs:
if d == "__MACOSX":
shutil.rmtree(os.path.join(root, d))
# get list of files and copy them all to root directory
filelist = []
for root, dirs, files in os.walk(os.getcwd()):
filelist += [os.path.join(root, f) for f in files]
for f in filelist:
shutil.move(f, os.path.join(os.getcwd(), f.split('\\')[-1].split('/')[-1].split('\\\\')[-1]))
# remove all subdirectories
for root, subdirs, files in os.walk(os.getcwd()):
for d in subdirs:
shutil.rmtree(os.path.join(root, d))
# process
print('\n\n--- Results ---')
try:
with open(os.path.join(os.getcwd(), 'one.txt'), 'r') as f:
one = f.read().splitlines()[0].lower().strip()
with open(os.path.join(os.getcwd(), 'two.txt'), 'r') as f:
two = f.read().splitlines()[0].lower().strip()
if one == 'one' and two == 'two':
print('Pass!')
else:
print('Fail!')
print('\tDo not contain correct text.')
except FileNotFoundError:
print('Fail!')
print('\tFiles not found. Check zip file and file names.')
except IndexError:
print('Fail!')
print('\tEmpty file. Need to add text to files and save.')
# remove everything
for root, subdirs, _files in os.walk(os.getcwd()):
for d in subdirs:
shutil.rmtree(os.path.join(os.getcwd(), d))
for f in _files:
if 'zipit.py' != f:
os.remove(os.path.join(os.getcwd(), f))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment