Unable to zipfiles within folders using Python
Question: I have multiple folders within a directory (D:/zptest). Each folder has many
files. I am trying to zip all these files with in that folder and save the
file in the same folder with the "foldername.zip" I have written a script for
this but unfortunately it's throwing the following error

    
    
    PermissionError: [Errno 13] Permission denied:
    

Can some body help me?

I am using the following code

    
    
    #!/usr/bin/env python
    import os, zipfile
    from pathlib import Path
    loc=r'D:\zipfilelist'
    p=Path(r'D:\zptest')
    for x in p.iterdir():
       x1=str(x)
       print("Processing: " + x1)
       dirs=os.listdir(x1)
       for file in dirs:
          file=os.path.join(x1,file)
          zip = zipfile.ZipFile(, "a", zipfile.ZIP_DEFLATED)
          zip.write(file)
          zip.close()
    


Answer: Thanks for all your help. I was able to complete a working code.

    
    
    #!/usr/bin/env python
    
    import os, zipfile, datetime, time
    from pathlib import Path
    
    p=Path(r'V:\NAR_ARCHIVE')
    for x in p.iterdir():
    x1=str(x)
    print("Processing: " + x1)
    #zipfilename = "%s.zip" % (x.stem)
    loc=r'Y:\StorageReports\NAR_Zips'
    loc=loc + '\\' + x.stem + '.zip'
    print (loc)
    print(x.stem)
    dirs=os.listdir(x1)
    
    #print(dirs)
    for file in dirs:
    sdate=datetime.datetime(2015,5,30)
    edate=datetime.datetime(2015,6,7)
    file=os.path.join(x1,file)
    crtime=time.ctime(os.path.getmtime(file))
    d1=datetime.datetime.strptime(crtime,"%a %b %d %H:%M:%S %Y")
    if d1<edate and d1>sdate:
    file=os.path.join(x1,file)
    print(file)
    #logFile = open(r"Y:\StorageReports\NAR_Zips\logfile.log","a")    
    #print >> logFile,message
    zip = zipfile.ZipFile(loc, "a", zipfile.ZIP_DEFLATED)
    zip.write(file)
    zip.close()
    

