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>You can grab all files from a folder without going into the subfolders using:</p>

<pre><code>import os
def getfilesfrom(directory):
    return filter(lambda x:
                  not os.path.isdir(os.path.join(directory, x)),
                  os.listdir(directory))

# or alternatively, using generators (as suggested in the comments):
def getfilesfrom(directory):
    for x in os.listdir(directory):
        if not os.path.isdir(os.path.join(directory, x)):
            yield x  # or yield os.path.join(directory, x) for full path.
</code></pre>

<p>You can then simply run (as described in the <a href="https://pymotw.com/2/zipfile/" rel="nofollow">documentation</a>):</p>

<pre><code>import datetime
import zipfile

def print_info(archive_name):
""" Print information from zip archive"""
zf = zipfile.ZipFile(archive_name)
for info in zf.infolist():
    print info.filename
    print '\tComment:\t', info.comment
    print '\tModified:\t', datetime.datetime(*info.date_time)
    print '\tSystem:\t\t', info.create_system, '(0 = Windows, 3 = Unix)'
    print '\tZIP version:\t', info.create_version
    print '\tCompressed:\t', info.compress_size, 'bytes'
    print '\tUncompressed:\t', info.file_size, 'bytes'
    print

print 'creating archive'
zf = zipfile.ZipFile('debug.zip', mode='a', compression=zipfile.ZIP_DEFLATED)
inputdir = '/sample/debug/'
filestozip = getfilesfrom(inputdir)
for afile in filestozip:
    print('adding ' + afile + ' to zipfile debug.zip')
    zf.write(os.path.join(inputdir, afile), afile)

print 'closing'
zf.close()

print
print_info('debug.zip')
</code></pre>

python windows zipfile