0

I am writing a Python script that takes user input in the form of a date eg 20180829, which will be a subdirectory name, it then uses the os.walk function to walk through a specific directory and once it reaches the directory that is passed in it will jump inside and look at all the directory's within it and create a directory structure in a different location.

My directory structure will look something like this:

|dir1

|-----|dir2|

|-----------|dir3

|-----------|20180829

|-----------|20180828

|-----------|20180827

|-----------|20180826

So dir3 will have a number of sub folders which will all be in the format of a date. I need to be able to copy the directory structure of just the directory that is passed in at the start eg 20180829 and skip the rest of directory's.

I have been looking online for a way to do this but all I can find is ways of Excluding directory's from the os.walk function like in the thread below: Filtering os.walk() dirs and files

I also found a thread that allows me to print out the directory paths that I want but will not let me create the directory's I want: Python 3.5 OS.Walk for selected folders and include their subfolders.

The following is the code I have which is printing out the correct directory structure but is creating the entire directory structure in the new location which I don't want it to do.

includes = '20180828'
inputpath = Desktop
outputpath = Documents

for startFilePath, dirnames, filenames in os.walk(inputpath, topdown=True):
    endFilePath = os.path.join(outputpath, startFilePath)
    if not os.path.isdir(endFilePath):
        os.mkdir(endFilePath)
    for filename in filenames:
        if (includes in startFilePath):
            print(includes, "+++", startFilePath)
            break
Hari Krishnan
  • 2,049
  • 2
  • 18
  • 29
murphy
  • 13
  • 1
  • 2

1 Answers1

1

I am not sure if I understand what you need, but I think you overcomplicate a few things. If the code below doesn't help you, let me know and we will think about other approaches.

I run this to create an example like yours.

# setup example project structure

import os
import sys

PLATFORM = 'windows' if sys.platform.startswith('win') else 'linux'
DESKTOP_DIR = \
    os.path.join(os.path.join(os.path.expanduser('~')), 'Desktop') \
    if PLATFORM == 'linux' \
    else os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')

example_dirs = ['20180829', '20180828', '20180827', '20180826']

for _dir in example_dirs:
    path = os.path.join(DESKTOP_DIR, 'dir_from', 'dir_1', 'dir_2', 'dir_3', _dir)
    os.makedirs(path, exist_ok=True)

And here's what you need.

# do what you want to do

dir_from = os.path.join(DESKTOP_DIR, 'dir_from')
dir_to = os.path.join(DESKTOP_DIR, 'dir_to')
target = '20180828'

for root, dirs, files in os.walk(dir_from, topdown=True):
    for _dir in dirs:
        if _dir == target:
            path = os.path.join(root, _dir).replace(dir_from, dir_to)
            os.makedirs(path, exist_ok=True)
            continue
Tom Wojcik
  • 5,471
  • 4
  • 32
  • 44
  • So the code that you provided above will create a folder called 20180828 in another location. What I am trying to do is create the whole directory structure from the original post, from dir1 to dir3 and then create only the required directory inside dir3 which in this case is 20180828. I then need to go inside 20180828 and create all directory's inside that directory. My problem is trying to ignore all the other folders in dir3 except 20180828. I hope i am explaining my self well. – murphy Aug 30 '18 at 13:39
  • @murphy Ahh, I see. I edited my answer. I hope it helps. There are many ways to do what you need. That's one of them. – Tom Wojcik Aug 30 '18 at 15:28