2

I wrote down this code:

import shutil

files = os.listdir(path, path=None)
for d in os.listdir(path):
    for f in files:
        shutil.move(d+f, path)

I want every folder in a given directory (path) with files inside, the files contained in that folder are moved to the main directory(path) where the folder is contained.

For Example: The files in this folder: C:/example/subfolder/ Will be moved in: C:/example/

(And the directory will be deleted.) Sorry for my bad english :)

surrexitt
  • 41
  • 2
  • 8

2 Answers2

7

This should be what you are looking for, first we get all subfolders in our main folder. Then for each subfolder we get files contained inside and create our source path and destination path for shutil.move.

import os
import shutil

folder = r"<MAIN FOLDER>"
subfolders = [f.path for f in os.scandir(folder) if f.is_dir()]

for sub in subfolders:
    for f in os.listdir(sub):
        src = os.path.join(sub, f)
        dst = os.path.join(folder, f)
        shutil.move(src, dst)
Max Kaha
  • 902
  • 5
  • 12
  • Thanks! All work, but it doesn't delete the subfolder, how can i delete it? – surrexitt Nov 10 '19 at 20:51
  • @surrexitt You could use `shutil.rmtree(sub)` after you are done iterating over all its files. – Max Kaha Nov 10 '19 at 20:56
  • 1
    Thanks, it work! Another thing, instead of
    I can only put directory directly, and not a variable, do you know why?
    – surrexitt Nov 10 '19 at 21:07
  • Late to the party, hehe... Instead of `folder = r"THIS FOLDER"` Try `folder = input("pick a folder : ")` – shongyang low Dec 26 '20 at 07:08
  • but what to do with duplicate files? – Singh May 02 '22 at 07:13
  • 1
    @Singh If you want to find out if two files have the same content I would probably hash the content as described [here](https://stackoverflow.com/questions/3431825/generating-an-md5-checksum-of-a-file) and from that create a list or dict that contains your hashvalues. Then check if your current file you are copying was already copied against this list/dict and do something with that information. – Max Kaha May 02 '22 at 08:02
  • The two files have different content with the same name, and they are in multiple folders, so I want to move all those files to a new folder but don't want to remove them, just rename them because they contain different content, any solution for that? – Singh May 02 '22 at 08:07
1

Here another example , using a few lines with glob

import os
import shutil
import glob

inputs=glob.glob('D:\\my\\folder_with_sub\\*')
outputs='D:\\my\\folder_dest\\'

for f in inputs:
    shutil.move(f, outputs)
GiovaniSalazar
  • 1,999
  • 2
  • 8
  • 15