我正在使用shutil python模块在linux redhat机器上复制文件和目录.
I am using the shutil python module to copy files and directories on a linux redhat machine.
我编写了以下方法,该方法包含两个参数:src(正在收集的文件或目录的路径)和目的地(将收集的日志/目录粘贴到的所需新路径). /p>
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")错误:IOError:[Errno 2]没有这样的文件或目录:'collectedLogs/testrun/logs/logger.xml'
The error: IOError: [Errno 2] No such file or directory: 'collectedLogs/testrun/logs/logger.xml'
如果要查找的文件或目录是src,那么我会理解此错误的含义,但这是导致错误的目标.我发现,引发错误的这一行代码在我的复制方法中进入了"shutil.copy(src,destination)"行.
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.
到目前为止,我的复制方法仅覆盖现有文件,并且如果存在现有目录,它将创建一个带有时间戳的新目录.在这种情况下,目标文件仍然不存在.那么,可能是什么问题呢?为什么我在DESTINATION路径中遇到此错误(通常我希望在SRC路径中看到这种错误).
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).
是否可能因为这是一个.xml文件?
Could it possibly be because this is an .xml file?
推荐答案当我收到此错误时,通常意味着其中一个文件夹不存在.
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:[错误2]没有这样的文件或目录:'backup/today/my_file.txt'
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.
更多推荐
Errno 2使用python shutil.py没有此类文件或目录用于文件目标
发布评论