Extract sigle file from zip file based on file extension

Jussi Piitulainen jussi.piitulainen at helsinki.fi
Fri Feb 10 03:59:19 EST 2017


loial writes:

> I need to be able to extract a single file from a .zip file in python.
>
> The zip file will contain many files. The file to extract will be the
> only .csv file in the zip, but the full name of the csv file will not
> be known.
>
> Can this be done in python?

Find the one member name that ends with ".csv". If the following
assignment crashes, it wasn't true that there is exactly one such.

with zipfile.ZipFile(path, "r") as f:
    [member] = [name for name in f.namelist() if name.endswith(".csv")]
    # extract member here now that you know its full name



More information about the Python-list mailing list