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

时间:2012-01-27 13:29:39

标签: python copy

我正在使用以下命令执行文件的多个副本:

shutil.copy2(oldFile, newFile)

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

dateCreated = os.path.getctime(newFile)

我认为这是由于函数为copy2,我认为这会传递元数据,所以我只用copy来尝试它无济于事。

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

3 个答案:

答案 0 :(得分:4)

在我刚试过的测试中,我看到以下行为:

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

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

  

我认为这是因为函数是copy2,它承载了我相信的元数据,所以我尝试了它只是复制无济于事。

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

答案 1 :(得分:1)

只需添加一条关于以下内容的说明:

  

要获得新的创建日期,您必须主动删除   在使用副本或创建新版本的文件之前使用   拷贝文件。否则,创建的日期将保留原始日期   创作时间。

如果有超过n个备份,我想编写一个脚本来备份某些文件和翻转文件。 手动删除现有文件时,例如通过来自其他CMD窗口的'del c:\ test.txt'然后运行脚本,然后运行进行备份的脚本,该文件获得新的ctime。否则,如果存在具有相同名称的文件并且您想要创建副本,则ctime将与已删除文件中的相同。

我写了一个例子: test.py:

import os
import shutil
import datetime

f_path = 'c:\\test.txt'

print f_path
print 'ctime = ' + str(datetime.datetime.utcfromtimestamp(os.path.getctime(f_path)))
print 'mtime = ' + str(datetime.datetime.utcfromtimestamp(os.path.getmtime(f_path)))

print ''

bkp_file = f_path + '.bkp'
print 'copying to ' + bkp_file
shutil.copyfile(f_path, bkp_file)
print 'ctime = ' + str(datetime.datetime.utcfromtimestamp(os.path.getctime(bkp_file)))
print 'mtime = ' + str(datetime.datetime.utcfromtimestamp(os.path.getmtime(bkp_file)))

print ''
print 'removing backup...'
os.remove(bkp_file)
print 'os.path.exists(bkp_file) = ' + str(os.path.exists(bkp_file))
print ''
print 'copying to ' + bkp_file
shutil.copyfile(f_path, bkp_file)
print 'ctime = ' + str(datetime.datetime.utcfromtimestamp(os.path.getctime(bkp_file)))
print 'mtime = ' + str(datetime.datetime.utcfromtimestamp(os.path.getmtime(bkp_file)))
print ''
print 'removing backup again...'
os.remove(bkp_file)
print 'os.path.exists(bkp_file) = ' + str(os.path.exists(bkp_file))

import tempfile
tmp = tempfile.mktemp(dir='c:\\')
print ''
print 'Created temp file name - ' + tmp

print 'Copying to temp file name...'
shutil.copyfile(f_path, tmp)
print 'ctime = ' + str(datetime.datetime.utcfromtimestamp(os.path.getctime(tmp)))
print 'mtime = ' + str(datetime.datetime.utcfromtimestamp(os.path.getmtime(tmp)))

print ''
print 'renaming file to the same name as the one already deleted'
shutil.move(tmp, bkp_file)

print 'ctime = ' + str(datetime.datetime.utcfromtimestamp(os.path.getctime(bkp_file)))
print 'mtime = ' + str(datetime.datetime.utcfromtimestamp(os.path.getmtime(bkp_file)))
文件从不存在(或在不同的CMD窗口中删除)时输出

c:\test.py 
c:\test.txt
ctime = 2014-04-30 14:43:41.649976
mtime = 2014-05-05 11:19:19.344976

copying to c:\test.txt.bkp
ctime = 2014-05-05 12:19:38.281976
mtime = 2014-05-05 12:19:38.282976

removing backup... os.path.exists(bkp_file) = False

copying to c:\test.txt.bkp
ctime = 2014-05-05 12:19:38.281976
mtime = 2014-05-05 12:19:38.284976

removing backup again... os.path.exists(bkp_file) = False

Created temp file name - c:\tmpn5ofid Copying to temp file name...
ctime = 2014-05-05 12:19:38.311976
mtime = 2014-05-05 12:19:38.312976

renaming file to the same name as the one already deleted 
ctime = 2014-05-05 12:19:38.281976
mtime = 2014-05-05 12:19:38.312976

C:\>
脚本在一个CMD窗口中第二次启动时输出

C:\>test.py
c:\test.txt
ctime = 2014-04-30 14:43:41.649976
mtime = 2014-05-05 11:19:19.344976

copying to c:\test.txt.bkp

ctime = 2014-05-05 12:19:38.281976  # ctime of old file
mtime = 2014-05-05 12:21:58.179976

removing backup... os.path.exists(bkp_file) = False

copying to c:\test.txt.bkp
ctime = 2014-05-05 12:19:38.281976  # ctime of old file that does not exist
mtime = 2014-05-05 12:21:58.190976

removing backup again... os.path.exists(bkp_file) = False

Created temp file name - c:\tmpzi2lp2 Copying to temp file name...
ctime = 2014-05-05 12:21:58.222976
mtime = 2014-05-05 12:21:58.222976

renaming file to the same name as the one already deleted
ctime = 2014-05-05 12:19:38.281976  # after renaming existing file it gets old ctime  
mtime = 2014-05-05 12:21:58.222976

C:\>

答案 2 :(得分:0)

使用:

shutil.copyfile(src, dst)

the docs:“[此副本]将名为src的文件的内容(无元数据)添加到名为dst的文件中。”