Python: Access 4th level of a zipped folder

Asked

Viewed 107 times

-2

Hello!

I’d like to access the fourth level of a zipped folder. I mean, I have a zip file and inside it there is a folder and inside this has another folder and so on... as graph below.

I’d like to grab a file whose extension is ". csv".

How can I do this directly?

I can already decompress, but I would have to do another code to navigate inside these folders. This I know how to do. I would just like to do the job at once, with a single code. Can you help me?

inserir a descrição da imagem aqui

  • 1

    has developed something?

  • This may help you: https://code.tutsplus.com/pt/tutorials/compressing-and-extracting-files-in-python-cms-26816

  • 2

    Once extracted, it would not be enough to read the file pasta1/pasta2/pasta3/pasta4/arquivo.csv? I don’t understand what the doubt is.

  • Thank you. In answer: The doubt is to extract only that file. What I will do later is not part of the question.

  • Maury Developer, I already know the site indicated. It does not help. The orientation there is about extracting files... I want to extract file that is within 4th level of compressed folder.

1 answer

2


Good afternoon, Wilson ! So dude, your question has some holes as to why you do what you’re asked and there are ways to develop that you don’t need automation when it comes to harvesting files inside directories, but still here’s something similar to what you asked for, I did in a few minutes, I used the image that attached the question as example, ie I created a.zip file -> pasta1 -> pasta2 -> pasta3 -> pasta4 -> Archive.csv.

The script extracts the.zip file and automatically collects the names of the directories and searches within each directory the existence of the file ".csv file", if it does not find it, searches inside another directory and so on, here is the code:

import zipfile
import os
from os import listdir
import glob

with zipfile.ZipFile(os.getcwd() +'/'+ listdir(os.getcwd())[1],'r') as zip:
    zip.extractall()

n = 0
cond = ''
filename = []
filename = listdir(os.getcwd())[0]

while cond != 'Arquivo.csv':

    try:
        filename = filename+'/'+listdir(filename)[0]
    except:
        pass
        cond = filename.split('/')[n]
        n += 1 



print(filename)

The output "Filename" will be the entire directory in which the ".csv file" is located and it is what you will use to manipulate such file, if you want to change so that the code looks for an extension ". csv" only, no specific name is only change in while.

As he goes identifying directories he goes saving in a list of name "cond" and analyzing the last item of this list, always to find the "file.csv". I hope I’ve helped !

  • I appreciate your attention my friend. The question is the same, extract only the "file.csv". I will study your code to solve an error that is giving me and check the final result. I really appreciate your contribution. In fact, the doubt written in this way is to facilitate, if I write a lot the staff will not understand. I’m trying to work with about 30 of these files and for that, I need to automate.

  • Got it, my man, good luck there !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.