Errno 2 with python shutil.py There is no such file or directory for file assignment

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

I wrote the following method, which takes two parameters: src (the path to the file or dir that is being collected) and destination (the desired new path into which the compiled log / dir file is inserted).

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 use this method just fine, but I recently encountered an error while running this code:

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

Error: IOError: [Errno 2] There is no such file or directory: 'collectLogs / testrun / logs / logger.xml'

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

So far, my copy method has simply overwritten existing files, and if there is an existing directory, it creates a new one with a timestamp. In this case, the target file does not exist yet. So what could be the problem? Why am I getting this error using the DESTINATION path (when I usually expect to see such an error using the SRC path).

Perhaps this is because it is a .xml file?

+2
python linux xml file shutil
Jul 29 '15 at 6:55
source share
2 answers

When I get this error, it usually means that one of the folders does not exist.

I wrote a simple script to check this out. In the script below, the backup folder exists, but not in the current folder. When I run the script, I get the same error as you.

IOError: [Errno 2] There is 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 exist, I would check that the permissions on them have not changed.

+2
Jul 29 '15 at 8:28
source share

By default, shutil.copytree() follows (resolves) symbolic links. If a symbolic link is broken, it throws a No such file or directory exception. One way is to indicate that symbolic links should be copied unresolved by passing symlinks=True .

0
May 29 '16 at 3:41
source share



All Articles