0

I have 2 directories:

dir

And in these 2 zip folders, 7 test files are present. I am trying this code to work on these:

import pandas as pd
import zipfile

from zipfile import ZipFile



def get_vcf_names(vcf_path):
    with ZipFile.open(vcf_path, "rt") as ifile:
          for line in ifile:
            if line.startswith("#CHROM"):
                  vcf_names = [x for x in line.split('\t')]
            break
    ifile.close()
    return vcf_names


names = get_vcf_names('VCFs_1.zip')
vcf = pd.read_csv('VCFs_1.zip', compression='zip', comment='#', chunksize=10000, delim_whitespace=True, header=None, names=names)

but its giving me:

AttributeError: 'str' object has no attribute 'fp'

James Z
  • 12,209
  • 10
  • 24
  • 44
  • You should use `with ZipFile(vcf_path, ”r”) as ifile` to open the ZIP file. Only then you can use `ifile.open(…)` to read files in the ZIP archive. See the [documentation](https://docs.python.org/3/library/zipfile.html#zipfile-objects). – bb1 Jan 15 '22 at 04:04
  • I tried: def get_vcf_names(vcf_path): with ZipFile(vcf_path, "r") as ifile: but its giving me error: TypeError: 'ZipFile' object is not iterable – Anas Jamshed Jan 15 '22 at 06:22
  • I assume you are trying to get the names of the files in the zip, you should take a look here https://stackoverflow.com/questions/8844781/get-file-list-of-files-contained-in-a-zip-file – Alik.Koldobsky Jan 15 '22 at 06:33

0 Answers0