miurahr / py7zr

7zip in python3 with ZStandard, PPMd, LZMA2, LZMA1, Delta, BCJ, BZip2, and Deflate compressions, and AES encryption.

Home Page:https://pypi.org/project/py7zr/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Documentation: Extraction of single files

LuminairPrime opened this issue · comments

This code snippit on the front page doesn't run for me. I think SevenZipFile needs to be py7zr.SevenZipFile, and I get this error about the brackets and "for":

selective_files = [f if filter_pattern.match(f) for f in allfiles]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: expected 'else' after 'if' expression

filter_pattern = re.compile(r'<your/target/file_and_directories/regex/expression>')
with SevenZipFile('archive.7z', 'r') as archive:
    allfiles = archive.getnames()
    selective_files = [f if filter_pattern.match(f) for f in allfiles]
    archive.extract(targets=selective_files)

I'm just learning, so I'm going through all this from the beginning. Thank you for the streamlined library!

Thank you for the feedback. It should be

    selective_files = [f for f in allfiles if filter_pattern.match(f)]