0

shutil模块包含几种复制文件的方法,但正如文档警告的那样,它们不会复制所有元数据,例如文件所有者和 ACL 不包含在窗口中。

可以调用命令行来绕过这个,例如:

subprocessl.call('copy src dst') 

有没有更多的pythonic方式来做到这一点?提前致谢!

4

1 回答 1

1

取自http://timgolden.me.uk/python/win32_how_do_i/copy-a-file.html
这将通过调用 win32 库来复制 ACL,本质上是在 Windows 中执行Ctrl+ 。C

import os
import win32file
import tempfile

filename1 = tempfile.mktemp (".txt")
open (filename1, "w").close ()
filename2 = filename1 + ".copy"
print filename1, "=>", filename2

#
# Do a straight copy first, then try to copy without
# failing on collision, then try to copy and fail on
# collision. The first two should succeed; the third
# should fail.
#
win32file.CopyFile (filename1, filename2, 1)
win32file.CopyFile (filename1, filename2, 0)
win32file.CopyFile (filename1, filename2, 1)

if os.path.isfile (filename2): print "Success"

dirname1 = tempfile.mktemp (".dir")
os.mkdir (dirname1)
dirname2 = dirname1 + ".copy"
print dirname1, "=>", dirname2

#
# The CopyFile functionality doesn't seem to cope
# with directories.
#
win32file.CopyFile (dirname1, dirname2, 1)

if os.path.isdir (dirname2): print "Success"
于 2013-10-22T07:54:00.537 回答