From b27a88ca431bba4fe5036ab54348367b45bc9cd3 Mon Sep 17 00:00:00 2001 From: Mariano Spadaccini Date: Tue, 7 Feb 2023 17:02:50 +0100 Subject: PWC 203 - Perl, Python, Go, Bash, Korn shell --- challenge-203/spadacciniweb/python/ch-2.py | 63 ++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 challenge-203/spadacciniweb/python/ch-2.py (limited to 'challenge-203/spadacciniweb/python/ch-2.py') diff --git a/challenge-203/spadacciniweb/python/ch-2.py b/challenge-203/spadacciniweb/python/ch-2.py new file mode 100644 index 0000000000..52ffb23269 --- /dev/null +++ b/challenge-203/spadacciniweb/python/ch-2.py @@ -0,0 +1,63 @@ +# Task 2: Copy Directory +# Submitted by: Julien Fiegehenn +# +# You are given path to two folders, $source and $target. +# Write a script that recursively copy the directory from $source to $target except any files. +# +# Example +# Input: $source = '/a/b/c' and $target = '/x/y' +# +# Source directory structure: +# +# ├── a +# │ └── b +# │ └── c +# │ ├── 1 +# │ │ └── 1.txt +# │ ├── 2 +# │ │ └── 2.txt +# │ ├── 3 +# │ │ └── 3.txt +# │ ├── 4 +# │ └── 5 +# │ └── 5.txt +# +# Target directory structure: +# +# ├── x +# │ └── y +# +# Expected Result: +# +# ├── x +# │ └── y +# | ├── 1 +# │ ├── 2 +# │ ├── 3 +# │ ├── 4 +# │ └── 5 + +import os +import re +import sys + +source = input("path source: ") +if not(os.path.exists(source)): + sys.exit("{0} directory does not exist.\n".format(source)) + +target = input("path target: ") +if not(os.path.exists(target)): + sys.exit("{0} directory does not exist.\n".format(target)) + +dirs = [] + +for path, subdirs, files in os.walk(source): + for subdir in subdirs: + if len(path) == len(source): + dirs.append(subdir) + else: + dirs.append( os.path.join(path[len(source)+1:], subdir)) + +for dir in dirs: + path = os.path.join(target, dir) + os.mkdir(path) -- cgit