【发布时间】:2014-05-06 15:44:05
【问题描述】:
我正在尝试更改 zip 文件中存在的所有文件和文件夹的最后修改时间。 如果我在解释器中逐行尝试脚本,最后修改时间似乎已更改。但同样没有反映在 zip 文件中。
我在 python 中使用 zipfile 模块。
下面是我的源代码。
import zipfile as zp
import datetime
import datetime
def main():
zipfile = raw_input("enter the path for your zipfile :")
if os.path.exists(zipfile) and zp.is_zipfile(zipfile):
time = raw_input("enter the time for which your files and folders in zip file want to be changed. format: dd-mm-yyyy HH:MM:SS -")
if time is not None :
time = datetime.datetime.strptime(time, "%d-%m-%Y %H:%M:%S")
newtime = time
time = (time.year, time.month, time.day, time.hour, time.minute, time.second)
print "time =", time
z = zp.ZipFile(zipfile, "a", zp.ZIP_DEFLATED)
z.printdir()
try :
for i in range(len(z.filelist)):
z.infolist()[i].date_time = time
z.printdir()
finally :
z.close()
else :
print "you have not entered a valid time!"
else :
print " you have not entered a valid zipfile name. "
if __name__ == "__main__":
main()
【问题讨论】:
标签: python python-2.7 datetime zipfile