将文件属性保留在副本上 [英] Keeping file attributes on a copy

查看:92
本文介绍了将文件属性保留在副本上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一种情况,希望将原始属性保留在文件中(文件创建日期等).通常,当您在Windows中复制文件时,您创建的副本会获得新的修改"日期等.我遇到了shutil.copy命令-尽管这不能使文件属性保持不变.

I have the situation whereby I want to keep the original attributes on a file (the file creation date etc). Normally when you copy files in Windows, the copy that you make gets new 'modified' dates etc. I have come accross the shutil.copy command — although this doesn't keep the file attributes the same.

我发现了

I found the following question on Stack Unix, but I was wondering if there was a way for me to do this in Python.

推荐答案

如果查看shutil的文档,您将立即找到

If you look at the documentation for shutil, you'll immediately find the copy2 function, which is:

copy()相同,除了copy2()还会尝试保留所有文件元数据.

Identical to copy() except that copy2() also attempts to preserve all file metadata.

在最新版本的Python中,有很多功能可以单独进行这些操作– copycopymodecopystat,但是如果您只想复制所有内容,则copy2可以完成所有操作可能.

In recent versions of Python, there's a whole slew of functions to do bits and pieces of this separately—copy, copymode, copystat—but if you just want to copy everything, copy2 does everything possible.

正如文档顶部的警告所述,一切皆有可能"并不代表所有内容,而是包含日期和其他属性.特别是:

As the warning at the top of the documentation says, "everything possible" doesn't mean everything, but it does include the dates and other attributes. In particular:

在Windows上,不会复制文件所有者,ACL和备用数据流.

On Windows, file owners, ACLs and alternate data streams are not copied.

如果您确实确实需要包含这些内容,则需要访问Win32 API(最简单的方法是通过

If you really need to include even that stuff, you will need to access the Win32 API (which is easiest to do via pywin32). But you don't.

这篇关于将文件属性保留在副本上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆