shutil 模块包含几种复制文件的方法,但是当文档警告它们不复制所有元数据(如文件所有者和ACL)时,窗口中不会包含这些文件。
可以调用命令行来绕过它,例如:
subprocessl.call('copy src dst')
有更多的pythonic方法吗?提前谢谢!
答案 0 :(得分: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"