vithika
vithika

Reputation: 233

Python script to copy different files from one folder to different sub-folders.with same name as file name

I have files (like 1, 2, 3, 4, 5, and so on) placed inside a directory ("docs"). Now I want to move these files to a different directory ("example") which should contain subfolders 1, 2, 3, 4, ... . The files should be placed accordingly (like subfolder 1 should contain the file 1 from the folder "docs," file 2 in subfolder 2, and so on). Please provide some pointers on how to achieve this using a Python script.

Upvotes: 1

Views: 56

Answers (1)

vithika
vithika

Reputation: 233

import os
import glob
import re
import os, sys
import io
from os.path import join, expanduser
j=1

RootDir1 = r'C:/users/hp/RadImagesFilpng/'
TargetFolder = r'C:/users/hp/Radpngcopy/copy'
for root, dirs, files in os.walk((os.path.normpath(RootDir1)), topdown=False):
        for name in files:
            if name.endswith('.html'):
                print ("Found")
                SourceFolder = os.path.join(root,name)
                shutil.copy2(SourceFolder, TargetFolder)

This works for me

Upvotes: 1

Related Questions