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 !
has developed something?
– Tmilitino
This may help you: https://code.tutsplus.com/pt/tutorials/compressing-and-extracting-files-in-python-cms-26816
– Maury Developer
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.– Woss
Thank you. In answer: The doubt is to extract only that file. What I will do later is not part of the question.
– Wilson Junior
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.
– Wilson Junior