-3

I have a big folder /myfolder/ that contains many different subfolders like:

  • /myfolder/aa/
  • /myfolder/ab/
  • /myfolder/ac/
  • /myfolder/ad/

and each of the subfolders contains many zip files. I would like to unzip all of them in the myfolder directly (all in the same place, that is)

How can I do that in Python? Thanks!

ℕʘʘḆḽḘ
  • 18,566
  • 34
  • 128
  • 235
  • that's a programming question bro. I can do it in Python if you like – ℕʘʘḆḽḘ Apr 10 '19 at 01:38
  • So are you looking for a Python or an R solution? These multi-code-tag questions are always a bit "difficult" to digest. Better to stick to one language. – Maurits Evers Apr 10 '19 at 02:48
  • I wonder why you would comment on your on question with `that's a programming question bro. I can do it in Python if you like`, was it soliloquy? – beginer Apr 10 '19 at 03:47

2 Answers2

1

This could be done in python, but a bash script might be more ideal.

from glob import glob
import zipfile

zfiles = glob('/myfolder/*/*.zip')
for zpath in zfiles:
    zip = zipfile.ZipFile(zpath, 'r')
    zip.extractall('/'+'/'.join(zpath.split('/')[0:-1])+'/')
    zip.close()
bmsmith
  • 58
  • 7
0

I feel that a bash script might be better for you than a python script if you do this operation many times otherwise this question might be helpful to you.

How do you recursively unzip archives in a directory and its subdirectories from the Unix command-line?

evader110
  • 69
  • 6
  • thanks but I want all of my output in the root folder, which is not what happens in the question you linked – ℕʘʘḆḽḘ Apr 10 '19 at 01:41
  • You could simply add ```mv -v ~/new_directory/* ~/myfolder/``` then ```rm -rf ~/new_directory/``` afterwards. Which takes the contents from a output directory of the question I linked and moves it to where you want. – evader110 Apr 10 '19 at 01:53
  • 1
    It's helpful if your answer includes actual code and context, not just a link to another answer. See [answer] – camille Apr 10 '19 at 03:07