I would like to create a function that lists the items of a directory and adds just the files in a zip
folder (without having any folder inside the zip
). So my directory looks like as follows:
student_path
student_1:
file1.txt
file2.txt
file.html
student_2:
file1.txt
file2.txt
file.html
...
student_n:
file1.txt
file2.txt
I want to create a look through these folders that reads the files, deletes HTML files and zip only the text files of each student in a zip folder (but just the text files without any folder).
My code for this is as follows:
import os
import shutil
from zipfile import ZipFile, ZIP_DEFLATED
import pathlib
import pdb
path = "assignment1/student_files/"
folders = os.listdir(path)
# Create a zip file from a directory
def zipCreate(_path_, zip_path):
folder = pathlib.Path(_path_)
with ZipFile(zip_path, 'w', ZIP_DEFLATED) as zipf:
for file in folder.iterdir():
zipf.write(file, arcname=file.name)
# Remove all python files from the student_files directory
def remove_html(path):
for item in folders:
if not item == ".DS_Store":
for file in os.listdir(path + item):
if os.path.isdir(path + item + "/" + file):
shutil.rmtree(path + item + "/__pycache__/")
if file.endswith(".html"):
print("Removing " + path + item + "/" + file)
os.remove(path + item + "/" + file)
# Create a zip file from the student_files directory
zip_path = path + "/" + item + ".zip"
zipCreate(path+item+"/", zip_path)
if __name__ == "__main__":
remove_html(path)
print("Done removing python files and zipping submissions...")
However, each time I create a zip
file, it contains a folder directory named student_n
, while I just want to include the text files.
It is entirely unclear what you want to happen, and I have given up trying to get you to write a coherent question. In any case, the code below will start with the path given on the command line. For each subdirectory of that path, it will create a zip file with the name plus ".zip" in the current directory. Each such zip file will then have an entry written for each ".txt" file in the corresponding subdirectory, using just the name of that file. Or maybe you want not ent.name.endswith('.html')
? Not sure. Regardless, deleting all of the ".html" files seems unnecessarily violent, if all you're trying to do is make these zip files.
import os
import sys
import zipfile
with os.scandir(sys.argv[1]) as base:
for dir in base:
if dir.is_dir():
with zipfile.ZipFile(dir.name + '.zip', 'w') as zip:
with os.scandir(dir.path) as ents:
for ent in ents:
if ent.is_file() and ent.name.endswith('.txt'):
zip.write(ent.path, ent.name)
For your example the command line argument would be "student_path". Then this will create student_1.zip containing file1.txt and file2.txt, and similarly student_2.zip and student_3.zip.