我正在使用以下命令对一个文件进行多个副本:
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.
要获得新的创建日期,您必须在使用 copy
或 copyfile
创建文件的新版本之前主动删除该文件。否则,创建日期
将保留其原始创建时间。 copyfile
在date creation
方面引发与copy
相同的行为。
关于python - 复制没有先前元数据的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9033958/