python - 复制没有先前元数据的文件

标签 python copy

我正在使用以下命令对一个文件进行多个副本:

shutil.copy2(oldFile, newFile)

它不会返回新创建文件的创建日期,而是保留旧文件。我正在检索日期:

dateCreated = os.path.getctime(newFile)

我认为这是因为函数是 copy2,我相信它会携带元数据,所以我尝试仅使用 copy 无济于事。

但奇怪的是,Windows 资源管理器中的“已修改数据”选项卡显示正确的日期和时间。

最佳答案

在我刚才尝试的测试中,我看到了以下行为:

test.txt -> Created: Tuesday, January 24, 2012 2:52 PM
         -> Modified: Tuesday, January 24, 2012 2:52 PM

>>> from shutil import *
>>> copy('test.txt','test1.txt')

目录中没有预先存在的 test1.txt 版本,我得到:

test1.txt -> Created: Today 8:54 AM
          -> Modified: Today 8:54 AM

然后我删除 test1.txt 并运行:

>>> copy2('test.txt','test1.txt')

目录中没有预先存在的 test1.txt 版本,我得到:

test1.txt -> Created: Tuesday, January 24, 2012 2:52 PM
          -> Modified: Tuesday, January 24, 2012 2:52 PM

然后我运行:

>>> copy('test.txt','test1.txt')

因此,在我得到的目录中有一个预先存在的 test1.txt 版本:

test1.txt -> Created: Tuesday, January 24, 2012 2:52 PM
          -> Modified: Today 9:00 AM

然后我运行:

>>> copy('test.txt','test1.txt')

因此,在我得到的目录中有一个预先存在的 test1.txt 版本:

test1.txt -> Created: Tuesday, January 24, 2012 2:52 PM
          -> Modified: Today 9:01 AM

这是您所看到的行为,您的引用:

I thought this was due to the function being copy2 which carries over meta data I believe, so I tried it with just copy to no avail.

要获得新的创建日期,您必须在使用 copycopyfile 创建文件的新版本之前主动删除该文件。否则,创建日期 将保留其原始创建时间。 copyfiledate creation 方面引发与copy 相同的行为。

关于python - 复制没有先前元数据的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9033958/

相关文章:

mysql - 复制行并插入源表

python - QtWebEngine 拦截带有自定义 URL 模式服务回复的请求

c# - 如何在CMD中使用SET和COPY?

python - wxpython中的wxThumbnailCtrl如何使用?

python - Flask - (ProgrammingError) - 构建postgresql数据库时的外键约束

xcode - 在Xcode中复制文件

postgresql - 带文字定界符的 Postgres COPY 命令

C++索引超出范围但没有在应该发生的时候发生

python - 在Python中比较同一字典中每个键的字典值

python绘图通过groupby过滤