Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Errno 2 using python shutil.py No such file or directory for file destination

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?

like image 962
OMGitzMidgar Avatar asked Jul 29 '15 06:07

OMGitzMidgar


People also ask

What is the use of shutil in python?

copy() method in Python is used to copy the content of source file to destination file or directory. It also preserves the file's permission mode but other metadata of the file like the file's creation and modification times is not preserved.

What is the difference between shutil copy() and shutil copytree()? what function is used to rename files give examples of each function?

While shutil. copy() will copy a single file, shutil. copytree() will copy an entire folder and every folder and file contained in it.

What is shutil?

Source code: Lib/shutil.py. The shutil module offers a number of high-level operations on files and collections of files. In particular, functions are provided which support file copying and removal. For operations on individual files, see also the os module.


1 Answers

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.

like image 123
Eric Bulloch Avatar answered Sep 20 '22 23:09

Eric Bulloch