Question

I am trying to copy a source code tree using the below code and running into an error,am not sure why I am getting this?error says \\Ref\builds/out exists but it doesnt exist,"out" is the directory the source location that the script is trying to copy to destination,any other ways to do copy if shutil is not suited for this type of copy?

//local/mnt/workspace/04.01_HY11/out
\\Ref\builds/out
copying
Traceback (most recent call last):
  File "test.py", line 21, in <module>
    main()
  File "test.py", line 18, in main
    copytree(src,dst)
  File "test.py", line 11, in copytree
    shutil.copytree(s, d)
  File "/pkg/qct/software/python/2.5.2/.amd64_linux26/lib/python2.5/shutil.py", line 110, in copytree
    os.makedirs(dst)
  File "/pkg/qct/software/python/2.5.2/.amd64_linux26/lib/python2.5/os.py", line 171, in makedirs
    mkdir(name, mode)
OSError: [Errno 17] File exists: '\\\\Ref\\builds/out'

Python code

import os,shutil

def copytree(src, dst, symlinks=False, ignore=None):
    for item in os.listdir(src):
        s = os.path.join(src, item)
        print s
        d = os.path.join(dst, item)
        print d
        if os.path.isdir(s):
            print "copying"
            shutil.copytree(s, d, symlinks, ignore)
        else:
            shutil.copy2(s, d)
def main ():
    src="//local/mnt/workspace/04.01_HY11"
    dst="\\\\Ref\\builds"
    copytree(src,dst)

if __name__ == '__main__':
    main()
Était-ce utile?

La solution

The documentation for shutil.copytree clearly says:

The destination directory, named by dst, must not already exist; it will be created as well as missing parent directories.

But \\ref\builds\out already exists—you can see from the exception's stack trace that it's trying to mkdir that path, but that's failing with an error indicating that that path already exists (which can happen when the path exists as either a regular file or a directory).

You need to copy to path that doesn't already exist, by either choosing a different path, or by deleting the existing tree at that location first. The latter can be done with shutil.rmtree.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top