2

I have a python service which at some point, will copy a directory from one location to another. When the service first runs, it raises an error "[Errno 2] No such file or directory" reporting the destination as the issue.

[Errno 2] No such file or directory: u'/opt/app/Gemfile.lock'

I'm not expecting the destination to be there, because I haven't copied it yet. Reading the documentation for distutils it says that if a path doesn't exist, it will make it for you.

#! /usr/bin/env python
import distutils.core
import os

files = []
file = {}
file['source'] = "/origin/folder"
file['destination'] = "/destionation/folder"
files.append(file)

def copy_files(files, logger):
    for file in files:
        if file['source'].startswith('/'):
            source = os.path.join(deployment.archive_dir, file['source'][1:])
        else:
            source = os.path.join(deployment.archive_dir, file['source'])
        if os.path.isdir(source):
            distutils.dir_util.copy_tree(source, file['destination'])
        else:
            if not os.path.isdir(file['destination']):
                distutils.dir_util.mkpath(file['destination'])
            distutils.file_util.copy_file(source, file['destination'])

copy_files(files)

This is the line that is throwing:

distutils.dir_util.copy_tree(source, file['destination'])
BubbleHulk
  • 78
  • 8

1 Answers1

-1

The problem is, you did not check file['destination'] first so because It does not exist, It cause to error raising. So beside checking is.dir(source), you should check is.dir(file['destination']) too.

pooya
  • 155
  • 6
  • Shouldn't it create the destination if it's not a directory? https://docs.python.org/2/distutils/apiref.html?highlight=copy_tree#distutils.dir_util.copy_tree – BubbleHulk Jul 14 '17 at 11:33
  • look at this post : https://stackoverflow.com/questions/10047521/how-to-copy-directory-with-all-file-from-c-xxx-yyy-to-c-zzz-in-python – pooya Jul 14 '17 at 11:45
  • Should I be using shutil over disutils? – BubbleHulk Jul 17 '17 at 09:46
  • I dont know you can try that and see if it works for you or not. Or as I said first Check destination and see is it a correct path or not(as you did with src. because maybe your file path format is wrong) – pooya Jul 17 '17 at 10:27