Search code examples
pythonzip

Look for content in csv files in a jar directory


I am trying to use ZipFile to peek into a jar directory, and look for a certain content within the the csv files in that directory. So far I have:

def check_variable(jar_path):
    with zipfile.ZipFile(jar_path, 'r') as zipf:
        ret_reader = csv.reader(zipf)
        for row in ret_reader:
            if row[0] == 'Variable Label':
                return row[1]
    raise OutputError('Variable Label not found in file %s!' % ret_file_name)

This doesn't seem to work. Can anyone please point me in the right direction? Thank you!


Solution

  • zipfile.ZipFile(jar_path, 'r') return a ZipFile object. So you need to open the csv file in the zip file.

    with zipfile.ZipFile(jar_path, 'r') as zipf:
        with zipf.open(csv_path, 'r') as csvpf: