Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy folder structure under another directory?

I have some questions related to copying a folder structure. In fact, I need to do a conversion of pdf files to text files. Hence I have such a folder structure for the place where I import the pdf:

D:/f/subfolder1/subfolder2/a.pdf 

And I would like to create the exact folder structure under "D:/g/subfolder1/subfolder2/" but without the pdf file since I need to put at this place the converted text file. So after the conversion function it gives me

D:/g/subfolder1/subfolder2/a.txt

And also I would like to add if function to make sure that under "D:/g/" the same folder structure does not exist before creating.

Here is my current code. So how can I create the same folder structure without the file?

Thank you!

import converter as c
import os
inputpath = 'D:/f/'
outputpath = 'D:/g/'

for root, dirs, files in os.walk(yourpath, topdown=False):
    for name in files:
      with open("D:/g/"+ ,mode="w") as newfile:
          newfile.write(c.convert_pdf_to_txt(os.path.join(root, name)))
like image 906
SXC88 Avatar asked Nov 27 '16 11:11

SXC88


People also ask

How can we copy an entire directory under another directory?

Similarly, you can copy an entire directory to another directory using cp -r followed by the directory name that you want to copy and the name of the directory to where you want to copy the directory (e.g. cp -r directory-name-1 directory-name-2 ).

What is the command to copy directory structure?

In order to copy the content of a directory recursively, you have to use the “cp” command with the “-R” option and specify the source directory followed by a wildcard character. Given our previous example, let's say that we want to copy the content of the “/etc” directory in the “/etc_backup” folder.

How do I copy multiple folders from one directory to another?

Once the files are visible, press Ctrl-A to select all of them, then drag and drop them to the right location. (If you want to copy the files to another folder on the same drive, remember to hold down Ctrl while you drag and drop; see The many ways to copy, move, or delete multiple files for details.)


3 Answers

For me the following works fine:

  • Iterate over existing folders

  • Build the structure for the new folders based on existing ones

  • Check, if the new folder structure does not exist
  • If so, create new folder without files

Code:

import os

inputpath = 'D:/f/'
outputpath = 'D:/g/'

for dirpath, dirnames, filenames in os.walk(inputpath):
    structure = os.path.join(outputpath, dirpath[len(inputpath):])
    if not os.path.isdir(structure):
        os.mkdir(structure)
    else:
        print("Folder does already exits!")

Documentation:

  • os.walk
  • os.mkdir
  • os.path.isdir
like image 168
linusg Avatar answered Oct 19 '22 19:10

linusg


How about using shutil.copytree()?

import shutil
def ig_f(dir, files):
    return [f for f in files if os.path.isfile(os.path.join(dir, f))]

shutil.copytree(inputpath, outputpath, ignore=ig_f)

The directory you want to create should not exist before calling this function. You can add a check for that.

Taken from shutil.copytree without files

like image 31
kumardeepakr3 Avatar answered Oct 19 '22 20:10

kumardeepakr3


A minor tweak to your code for skipping pdf files:

for root, dirs, files in os.walk('.', topdown=False):
    for name in files:
        if name.find(".pdf") >=0: continue
        with open("D:/g/"+ ,mode="w") as newfile:
            newfile.write(c.convert_pdf_to_txt(os.path.join(root, name)))
like image 1
VBB Avatar answered Oct 19 '22 19:10

VBB