murphy
murphy

Reputation: 13

Python os.walk Include only specific folders

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

Upvotes: 0

Views: 3574

Answers (1)

Tom Wojcik
Tom Wojcik

Reputation: 6179

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

Upvotes: 1

Related Questions