-3

I am looking for help with a Python program/script to remove all folders but keep all files in a folder.

I have a big folder with 100 folders each one containing another folder with an mp3 file inside and I would extract all the mp3s in one big folder.

My guess would be for each folder... if it has another folder inside (and it has), delete both folders so that only the endfile (mp3) remains in the big home folder.

Could anyone help me without losing the files?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65

1 Answers1

0

Try like this:

import os
import shutil

os.chdir("PATH")
for x in os.listdir():
  if os.path.isdir(x):
    shutil.rmtree(x)
Wasif
  • 14,755
  • 3
  • 14
  • 34
  • 1
    It deletes eveyrhint in the homefolder. What kind of silly joke is this? Want report? – Sergiu Pascu Oct 10 '20 at 10:39
  • No it will check if the object is a file or folder and if folder then delete. Only reason is wrong implementation of snippet. – Wasif Oct 10 '20 at 15:41