0

I wanted to try copying a directory's contents to another directory with just a partial filepath, and couldn't get it to work myself with this code;

srcdir = "\a\b\c"
destdir = "\x\y\z"


shutil.copytree(srcdir, destdir)
print("Done")
time.sleep(0.5)
exit()

How do i make it so it can use only the last parts of the filepath and match them to an existing, full filepath on the system?

Sokol1an
  • 1
  • 1
  • 1
    Are you asking about [relative path](https://stackoverflow.com/questions/40416072/reading-file-using-relative-path-in-python-project/40416154)? – PM 77-1 Nov 05 '20 at 15:05

1 Answers1

1

Do you mean something like this?

import shutil

basepath = '/Users/user_name/test'  # <------ the common folder for both trees
srcdir   = basepath + '/a/b/c'
destdir  = basepath + '/x/y/z'

shutil.copytree(srcdir, destdir, dirs_exist_ok=True)

It works fine if you have the common folder. If you don't have the common folder you can get it with many ways. It depends on what exactly you're trying to do.

Yuri Khristich
  • 13,448
  • 2
  • 8
  • 23