I'm doing multiple copies of a file with the following command:
shutil.copy2(oldFile, newFile)
And rather than returning the creation date of the newly created file, it's keeping the older one. I'm retrieving the date with:
dateCreated = os.path.getctime(newFile)
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.
However the odd thing is that the 'Data Modified' tab in a Windows Explorer is showing the correct date and time.
In the test I tried just now, I see the following behaviour:
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')
With no pre-existing version of test1.txt in the directory I get:
test1.txt -> Created: Today 8:54 AM
-> Modified: Today 8:54 AM
I then delete test1.txt and run:
>>> copy2('test.txt','test1.txt')
With no pre-existing version of test1.txt in the directory I get:
test1.txt -> Created: Tuesday, January 24, 2012 2:52 PM
-> Modified: Tuesday, January 24, 2012 2:52 PM
I then run:
>>> copy('test.txt','test1.txt')
So with a pre-existing version of test1.txt in the directory I get:
test1.txt -> Created: Tuesday, January 24, 2012 2:52 PM
-> Modified: Today 9:00 AM
I then run:
>>> copy('test.txt','test1.txt')
So with a pre-existing version of test1.txt in the directory I get:
test1.txt -> Created: Tuesday, January 24, 2012 2:52 PM
-> Modified: Today 9:01 AM
This is the behaviour you're seeing, your quote:
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.
To get a new creation date you're going to have to actively remove the file before you create a new version of it using either copy
or copyfile
. Otherwise the date created
will remain from it's original time of creation. copyfile
evokes the same behaviour as copy
with respect to date creation
.
Just to add one note regarding:
To get a new creation date you're going to have to actively remove the file before you create a new version of it using either copy or copyfile. Otherwise the date created will remain from it's original time of creation.
I wanted to write a script that makes a backup of some files and rollover files if there are more then n backups. When you remove the existing file manualy e.g. by 'del c:\test.txt' from other CMD window then running the script, and then run the script that makes a backup, the file gets new ctime. Otherwise, if the file with same name existed and you want to create a copy, the ctime will be somehow the same as in the deleted file.
I wrote an example: 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)))
output when file never existed (or deleted in different CMD window)
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:\>
output when script started second time in one CMD window
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:\>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With