How to zip only the files inside the folder and not the sub folders?
<p>I have a folder structure as:</p>

<pre><code>/sample/debug/
    --debug.exe
    --sample.exe
    --sample.pdb
    --debug.pdb
    --sample.dll
    --debug.dll
    /config
        --sample.txt
        --new.txt
    /general
        --general.txt
        --code.txt
</code></pre>

<p>So, what I want is only to zip the files inside debug and not the subfolders like /config and /general. I tried as follows:</p>

<pre><code>import zipfile
import os

def append( dir_name ):
    ret_val = []
    fileList = []
    for file in os.listdir(dir_name):
        try:
            dirfile = os.path.join(dir_name, file)
        except Exception:
            err = sys.exc_info()
            print ("Error!", err)
        fileList.append(dirfile)
        ret_val = fileList
    return ret_val

def zip( fileList, archive, root ):
    ret_val = 0
    try:
        zip_folder_contents = zipfile.ZipFile(archive, 'w', zipfile.ZIP_DEFLATED)
    except Exception:
        err = sys.exc_info()
        print ("Error!",err)
        exit( 1 )
    for filename in fileList:
        zip_folder_contents.write(filename,
                filename[len(root):].lstrip(os.path.sep).lstrip(os.path.altsep))
    zip_folder_contents.close()
    return ret_val

make(append_files_in_zipfolder("D:/sample/debug"), "debug.zip",
                               "D:/sample/debug")
</code></pre>

<p>Now when I execute above I get error as permission denied "D:/sample/debug\\config". Hence, I am not able to remove this error so I thought to only include the files inside the zip folder and exclude the subfolders. So, is there anyway how to do that or some way I can remove this permission related error I am getting. Please suggest.</p>

<p>The following makes use of the new ZipFile context manager available:</p>

<pre><code>from zipfile import ZipFile

def zip_folder(zip_name, folder):
    with ZipFile(zip_name, 'w') as myzip:
        for entry in os.listdir(folder):
            if os.path.isfile(entry):
                myzip.write(os.path.join(folder, entry), arcname=entry)

zip_folder(r'D:\output.zip', r'D:\sample\debug')
</code></pre>

python windows zipfile