-1

I have a code that copy folders and files from a given path that includes pdf files and word files and folder that includes pdf files also.

what i need is to copy each one to a different destination mean

  • the pdf file to destination a
  • the word file to destination b
  • the folder & subdiretories a destination c

the problem is that the destination a will have the pdf files and the subdirectories files that are in the folder. yet the destination c also will have the folder copied correctly.

so the problem is with the destination a

This is the structure I have in I:

I:
├── file1.doc
├── file1.pdf
├── file2.pdf
└── folder1
    ├── file3.pdf
    └── file4.pdf

Result must be:

├── A
│   ├── file1.pdf
│   └── file2.pdf
├── B
│   └── file1.doc
└── C
    └── folder1
        ├── file3.pdf
        └── file4.pdf

The outcome for now is :

├── A
│   ├── file1.pdf
│   ├── file2.pdf
│   ├── file3.pdf
│   └── file4.pdf
├── B
│   └── file1.doc
└── C
    └── folder1
        ├── file3.pdf
        └── file4.pdf

And this is the code I'm using:

import os
import shutil
from os import path

src = "I:/"
src2 = "I:/test"

dst = "C:/Users/xxxx/Desktop/test/"
dst2 = "C:/Users/xxxxx/Documents/"
dst3 = "C:/Users/xxxxx/Documents/Visual Studio 2017"

def main():
    copy()

def copy():
    i = 0
    j = 0
    for dirpath, dirnames, files in os.walk(src):

        print(f'Found directory: {dirpath}')
        # for file_name in files:
        if len(dirnames)==0 and len(files)==0:
                print("this directory is empty")
        else:
            print(files)

        for name in files:

            full_file_name = os.path.join(dirpath, name)
            #check for pdf extension
            if name.endswith("pdf"):
                i=i+1
                #copy files
                shutil.copy(full_file_name, dst2)
                #check for doc & docx extension 
            elif name.endswith("docx") or name.endswith("doc"):
                j=j+1
                #copy files
                shutil.copy(full_file_name, dst3)
                # print(i,"word files done")
        print("{0} pdf files".format(i))
        print("{0} word files".format(j))


    # except Exception as e:
        # print(e)
    if os.path.exists(dst): 
        shutil.rmtree(dst)
        print("the deleted folder is :{0}".format(dst))
        #copy the folder as it is (folder with the files)
        copieddst = shutil.copytree(src2,dst)
        print("copy of the folder is done :{0}".format(copieddst))
    else:
        #copy the folder as it is (folder with the files)
        copieddst = shutil.copytree(src2,dst)
        print("copy of the folder is done :{0}".format(copieddst))

if __name__=="__main__":
    main()
w3Develops
  • 358
  • 1
  • 3
  • 15
Dev Dj
  • 169
  • 2
  • 14
  • Might [this answer](https://stackoverflow.com/a/12686557/8103477) be useful? – David Foster Feb 05 '20 at 10:58
  • @DavidFoster NO ITS NOT THE willing answer – Dev Dj Feb 05 '20 at 11:05
  • So you do not want to copy pdf files in subdirectories into folder A ? Copy only the pdf files that are in your source path ? – Prashant Kumar Feb 05 '20 at 11:13
  • @DevDj please clarify the description of your question. It's not clear what you expect the outcome to be. – David Foster Feb 05 '20 at 11:44
  • i will add the clarification to my question – Dev Dj Feb 05 '20 at 12:31
  • The actual outcome seems to fit exactly your rules: `*.pdf` in `A`, `*.doc` in `B`, and folders in `C`. Also, in the description of the result you want, where are `file3.pdf` and `file4.pdf`? Another thing you need to clarify is how to deal with duplicates, e.g. `folder1/duplicate.pdf` and `folder2/duplicate.pdf`. – accdias Feb 05 '20 at 12:55
  • @accdias except that its copying the folder to C AND its subdirectories file are copied to A and C – Dev Dj Feb 05 '20 at 13:00
  • @DevDj, edit your question and show the results in the examples with exactly the inputs you are using in the question. Also, for clarity, try to use a [tree structure](http://mama.indstate.edu/users/ice/tree/). If you don't know how to do it, I can edit it for you but then you will need to check after to see if I got it right. – accdias Feb 05 '20 at 13:10
  • 1
    I just edit your question and replaced the inputs and outputs with tree a like structures. See if they reflect your original problem. If they do, then the solution is trivial. – accdias Feb 05 '20 at 13:22

1 Answers1

0

Here is a simple solution in Python 3.4+ (because of pathlib):

from pathlib import Path

src = Path('I:/')

pdfs = Path('I:/test')
docs = Path('C:/Users/xxxxx/Documents')
dirs = Path('C:/Users/xxxxx/Documents/Visual Studio 2017')

suffixes = {
    'docs': ['.doc', '.docx'],
    'pdfs': ['.pdf']
}


def main():
    for o in src.glob('*'):
        if o.is_dir():
            o.rename(dirs / o.name)

        if o.is_file():
            if o.suffix in suffixes['docs']:
                o.rename(docs / o.name)
            elif o.suffix in suffixes['pdfs']:
                o.rename(pdfs / o.name)


if __name__ == '__main__':
    main()
accdias
  • 5,160
  • 3
  • 19
  • 31