aboutsummaryrefslogtreecommitdiff
path: root/challenge-203/spadacciniweb/python/ch-2.py
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-203/spadacciniweb/python/ch-2.py')
-rw-r--r--challenge-203/spadacciniweb/python/ch-2.py63
1 files changed, 63 insertions, 0 deletions
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)