diff options
| author | 冯昶 <fengchang@novel-supertv.com> | 2023-02-13 15:26:44 +0800 |
|---|---|---|
| committer | 冯昶 <fengchang@novel-supertv.com> | 2023-02-13 15:26:44 +0800 |
| commit | 1d7da338f23a3842739e903bfd4e06a04f341c11 (patch) | |
| tree | 1b896c54af3cc17ab0db3c1ce1c181fbd57c1ad2 /challenge-203/spadacciniweb/python/ch-2.py | |
| parent | 9d964b9bb2fc6df2fcaa6018fa6369f582996dab (diff) | |
| parent | 5ccac734c46826df9dedf843701fb1514175c2a6 (diff) | |
| download | perlweeklychallenge-club-1d7da338f23a3842739e903bfd4e06a04f341c11.tar.gz perlweeklychallenge-club-1d7da338f23a3842739e903bfd4e06a04f341c11.tar.bz2 perlweeklychallenge-club-1d7da338f23a3842739e903bfd4e06a04f341c11.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-203/spadacciniweb/python/ch-2.py')
| -rw-r--r-- | challenge-203/spadacciniweb/python/ch-2.py | 63 |
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) |
