Forums

How to access a folder in a zip file?

Hello!

I'm a newbie in python and I was trying to access a folder inside a zip file and I also have issues with relative and absolute path. Thks in advance. In projects there a lot of urls (i'll read them later one by one and store them in a list/array so that I could work with it easily) here is the path: home/name/tutorial/prof/ks.zip/projects

should I use

file_name = "ks.zip"

or

file_location = os.path.dirname(os.path.abspath(__file__)) 
#print(file_location)  ?

# specifying the zip file name 
file_name = "ks.zip"

 # opening the zip file in READ mode 
with ZipFile(file_name, 'r') as zip:

 # printing all the contents of the zip file 
   zip.printdir()

 # extracting all the files 
   print('Extracting all the files now...') 
   zip.extractall() 
   print('Done!')

[edited by admin: formatting]

you should probably use the full path if you are worried about path issues.

but it should look like /home/name/tutorial/prof/ks.zip/ instead of home/name/tutorial/prof/ks.zip/

Hi Conrad, thks for formatting the code (how did you do that?). So you mean

::: # specifying the zip file name

file_name = "/home/name/tutorial/prof/ks.zip/"

# opening the zip file in READ mode

with ZipFile(file_name, 'r') as zip:

# printing all the contents of the zip file

zip.printdir()

# extracting all the files

print('Extracting all the files now...') zip.extractall()

print('Done!')

Hi, i have tried this but got nothing for output

directory = '/home/username/tutorial/prof/ks.zip/'

#for filename in os.listdir(directory):

for filename in os.path.dirname(directory):

   if filename.endswith(".html"):

       f = open(filename)

       lines = f.read()

       print (lines[10])

       continue

   else:

       continue

[edited by admin: formatting]

is ks.zip a file or a folder? maybe you could add some print statements on each line to see where your code gets to

Hi Conrad, ks.zip is a zip folder and inside there is another folder named projects with a lots of urls. Thks

Does /home/username/tutorial/prof/ks.zip/ actually exist? Are you using /home/username/tutorial/prof/ks.zip/ literally, or have you used your actual username? You need to use your username, not string username in the path.

Sure its the path where the folder ks.zip is located and of coz i wrote my username (i.e charly /home/charly /tutorial/prof/ks.zip/) instead of username. :)

Ok. What does

import os
directory = '/home/username/tutorial/prof/ks.zip/'
print(os.path.exists(directory))
print(os.listdir(directory))

show?

Hi Conrad, here is what i have with your example ...

Traceback (most recent call last): File "test.py", line 28, in <module> print(os.listdir(directory)) NotADirectoryError: [Errno 20] not a directory: '/home/my_username/tutorial/prof/ks.zip/'

So I have tried something in Windows (because i worked in Ubuntu) And the code below works in windows with a normal folder and not with a zip one. so i have created two folders urls und urls.zip with links inside.

from os import listdir from os.path import isfile, join

mypath = 'U:/urls/' #not working with 'U:/urls.zip/'

content_list = [f for f in listdir(mypath) if isfile(join(mypath, f))]

print (content_list)

output:

['link1.html', 'link2.html', 'link3.html', 'link4.html', 'link5.html']

I dont know why it doesnt work in Ubuntu specially to access the ks.zip folder where there is another folder 'projects'

to reach the folder projects manually i first clicked on Files tab -> Home then clicked on tutorial then clicked on Prof then clicked on ks.zip finally clicked on projects

Ok. So ks.zip is not a directory. What about:

import os
from zipfile import ZipFile
zip_file = '/home/username/tutorial/prof/ks.zip'
with ZipFile(zip_file, "r") as z:
    print(z.infolist())

Hey,

here we go

<ZipInfo filename='projects/escala-scale-ruler-fountain-pen.1547746117370.faqs' compress_type=bzip2 filemode='-rw-rw-r--' file_size=198605 compress_size=58284>,

<ZipInfo filename='projects/hold-me-close-pet-carriers.1547745719648.updates' compress_type=bzip2 filemode='-rw-rw-r--' file_size=197959 compress_size=58153>,

... it prints infos about each link, what i didnt really want BUT i've done this way

zip_file ='/home/username/tutorial/prof/ks.zip'

with zipfile.ZipFile(zip_file,"r") as zip_ref: zip_ref.extractall("dezipped_folder")

and now i can reach the traget folder "dezipped_folder"

Thk you very much

may be i'm going to post again :)

OK, glad you worked out a solution!