[Python] 纯文本查看 复制代码
import os
import time
import re
import zipfile
import shutil
def findnewzip():
allfiles = os.listdir()
rarlist = []
retest = re.compile(r'emed.*\.zip')
workpath = os.getcwd()
for file in allfiles:
print(file)
if os.path.isfile(file):
if None != retest.match(file):
rarlist.append(os.path.join(workpath, file))
rarlist.sort(key=lambda f: os.path.getmtime(f))
return rarlist[-1]
if __name__ == '__main__':
print ('开始更新')
print(os.path.abspath(os.path.dirname(__file__)))
print(time.strftime('%Y-%m-%d', time.localtime(time.time())))
nowdate = time.strftime('%Y-%m-%d', time.localtime(time.time()))
foldername = 'emeditor'
oldfilename = foldername + '_' + nowdate
# 杀掉进程
os.system('taskkill /f /t /im emed*')
#复制文件夹
os.rename(foldername, oldfilename)
#找到压缩包
lastfilename = findnewzip()
print(lastfilename)
#新建目录
if False == os.path.exists(os.path.join(os.getcwd(),foldername)):
os.mkdir(foldername)
#解压文件
zipFile = zipfile.ZipFile(lastfilename, 'r')
for fileM in zipFile.namelist():
zipFile.extract(fileM, os.path.join(os.getcwd(),foldername))
zipFile.close();
#复制文件
shutil.copytree(os.path.join(os.getcwd(), oldfilename, 'AppData'), os.path.join(os.getcwd(), foldername, 'AppData'))
shutil.copytree(os.path.join(os.getcwd(), oldfilename, 'Help'), os.path.join(os.getcwd(), foldername, 'Help'))
shutil.rmtree(os.path.join(os.path.join(os.getcwd(), foldername, 'Macros')))
shutil.copytree(os.path.join(os.getcwd(), oldfilename, 'Macros'), os.path.join(os.getcwd(), foldername, 'Macros'))
shutil.copyfile(os.path.join(os.getcwd(), oldfilename, 'eeCommon.ini'), os.path.join(os.getcwd(), foldername, 'eeCommon.ini'))
shutil.copyfile(os.path.join(os.getcwd(), oldfilename, 'eeConfig.ini'), os.path.join(os.getcwd(), foldername, 'eeConfig.ini'))
shutil.copyfile(os.path.join(os.getcwd(), oldfilename, 'eeLM.ini'), os.path.join(os.getcwd(), foldername, 'eeLM.ini'))
shutil.copyfile(os.path.join(os.getcwd(), oldfilename, 'eePlugins.ini'), os.path.join(os.getcwd(), foldername, 'eePlugins.ini'))
shutil.copyfile(os.path.join(os.getcwd(), oldfilename, 'keygen.exe'), os.path.join(os.getcwd(), foldername, 'keygen.exe'))
print('升级结束')
os.system('pause')