18

I am using the shutil python module to copy files and directories on a linux redhat machine.

I wrote the following method, which takes in 2 params: src (the path of the file or dir that is being collected) and destination (the desired new path of where the collected log/dir is being pasted to).

def copy(src, destination):
    if(os.path.exists(src)):
        if(os.path.isdir(src)):
            if(os.path.exists(destination)):
                shutil.copytree(src, destination+getTimeStamp())
            else:
                shutil.copytree(src, destination)
        else:
            shutil.copy(src, destination)
    else:
        print src+" not found"

I have been using this method just fine, but I recently ran into an error when running this code:

copy("/home/midgar/logs/logger.xml", currentPath+"/testrun/logs/logger.xml")

The error: IOError: [Errno 2] No such file or directory: 'collectedLogs/testrun/logs/logger.xml'

I would understand what this error implies if the file or directory that it is looking for is the src, but this is the destination that is causing the error. I found out that this line of code that throws the error goes to the line: "shutil.copy(src, destination)" in my copy method.

So far, my copy method just overwrites existing files, and if there is an existing directory, it creates a new one with a time stamp. In this case, the destination file does not already exist anyways. So, what can be the issue? Why am I getting this error with the DESTINATION path (when I would typically expect to see this kind of error with the SRC path).

Could it possibly be because this is an .xml file?

OMGitzMidgar
  • 689
  • 2
  • 10
  • 28
  • note that both `home/midgar/logs` and `collectedLogs/testrun/logs/logger.xml` are relative paths. depending on your current working directory (`$PWD` in bash) this will have a different meaning. – hiro protagonist Jul 29 '15 at 07:38
  • That is a good point to make, I actually made an edit because I had a type (it should be "/home", not "home"). Also, the variable "currentPath" is the beginning of a full path, making both the src and dest a full path – OMGitzMidgar Jul 29 '15 at 07:41
  • assuming `/home/midgar/logs` is a directory, you are asking `shutil.copy` to copy a directory to a file. right? – hiro protagonist Jul 29 '15 at 07:48
  • Man I'm really sorry for all the errors in the initial post, I left something out again. I am actually pulling a file from "/home/midgar/logs/logger.xml". Of course, I edited the original post for this change. – OMGitzMidgar Jul 29 '15 at 08:01

3 Answers3

11

When I get this error it usually means that one of the folders doesn't exist.

I wrote a simple script to to test this out. In the script below the backup folder does exist but the today folder does not. When I run the script I get the same error as you.

IOError: [Errno 2] No such file or directory: 'backup/today/my_file.txt'

import shutil
shutil.copy("my_file.txt", "backup/today/my_file.txt")

If all of your folders do exist I would check to make sure the permissions on them have not changed.

Eric Bulloch
  • 827
  • 6
  • 10
  • Following this comment, I am almost positive that the issues lies in the fact that the "logs" directory that I am trying to put the xml file into also does not exist (just like your example with the today dir). I will try to modify the copy method so that it will handle the case where the destination source is more than just a file or dir (such as creating a new dir then putting the file inside). Thank you! – OMGitzMidgar Jul 29 '15 at 08:36
  • On stackexchange you should avoid 'Thank you' comments but you can mark the answer as correct :) – Eric Bulloch Aug 06 '15 at 09:18
3

By default, shutil.copytree() follows (resolves) symbolic links. If the symlink is broken, it raises a No such file or directory exception. One workaround is to specify that symlinks should be copied unresolved by passing symlinks=True.

Roger Dahl
  • 15,132
  • 8
  • 62
  • 82
1

I also recently came across this error. In my case, the file wouldn't be created because the filename and directory structure exceeded the max 260 characters. Solution: choose a shorter filename (or full file path name).