Search code examples
pythonzip

Python - using variable instead of direct file name


from zipfile import ZipFile
# Create a ZipFile Object and load sample.zip in it
with ZipFile('sampleDir.zip', 'r') as zipObj:
   # Extract all the contents of zip file in current directory
   zipObj.extractall()

I want to use a variable instead of sampleDir.zip so that when i place the above code inside for loop that will change the file name each time.


Solution

  • from zipfile import ZipFile
    # Create a ZipFile Object and load sample.zip in it
    var = 'sampleDir.zip'
    with ZipFile(var, 'r') as zipObj:
       # Extract all the contents of zip file in current directory
       zipObj.extractall()