欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python 提取文件的小程序

 更新時(shí)間:2009年07月29日 02:37:07   作者:  
在做網(wǎng)站項(xiàng)目時(shí),開(kāi)發(fā)經(jīng)常要給工程一個(gè)升級(jí)包,包含本次修改的內(nèi)容,這個(gè)升級(jí)包的內(nèi)容就是tomcat的發(fā)布目錄下的文件;
以前提取這些文件用的是一同事些的批處理文件;用起來(lái)不怎么順手,剛好最近在學(xué)些python,所有就自己動(dòng)手寫(xiě)了一個(gè)python提取文件的小程序;
1、原理
提取文件的原理很簡(jiǎn)單,就是到一個(gè)指定的目錄,找出最后修改時(shí)間大于給定時(shí)間的文件,然后將他們復(fù)制到目標(biāo)目錄,目標(biāo)目錄的結(jié)構(gòu)必須和原始目錄一致,這樣工程人員拿到后就可以直接覆蓋整個(gè)目錄;
2、實(shí)現(xiàn)
為了程序的通用,我定義了下面的配置文件
config.xml
復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<config>
    <srcdir>E:\temp\home\cargill</srcdir>
    <destdir>E:\temp\dest\cargill</destdir>
    <notinclude>
        <dirs>
            <dir>E:\temp\home\cargill\WEB-INF\lib</dir>
            <dir>E:\temp\home\cargill\static\cargill\report</dir>
        </dirs>
        <files>
            <file>E:\temp\home\cargill\WEB-INF\classes\myrumba.xml</file>
            <file>E:\temp\home\cargill\META-INF\context.xml</file>
        </files>
    </notinclude>
    <inittime>2008-10-11 13:15:22</inittime>
    <rardir>C:\Program Files\WinRAR</rardir>
</config>

其中
<srcdir>:原始目錄,即我們tomcat的發(fā)布目錄;
<destdir>:文件復(fù)制到得目標(biāo)目錄;
<notinclude>:需要忽略的文件夾和文件,具體需要忽略的內(nèi)容在其子節(jié)點(diǎn)中定義,這里不在解釋?zhuān)?
<inittime>:這個(gè)是初始化需要提取的時(shí)間點(diǎn),在這之后的才會(huì)提取,此處需要說(shuō)明,后來(lái)在使用中,我增加了一個(gè)功能,就是每次提取完會(huì)自動(dòng)將本次提取時(shí)間記錄到一個(gè)文本文件C_UPGRADETIME.txt中,這就省去每次設(shè)置這個(gè)值的煩惱,只有C_UPGRADETIME.txt為空或者不存在時(shí),才會(huì)用到這個(gè)值;
<rardir>:rar壓縮程序的地址;
下面是讀取配置文件的類(lèi):
config.py
復(fù)制代碼 代碼如下:

'''
Created on Mar 3, 2009
@author: alex cheng
'''
from xml.dom.minidom import parse, parseString
import datetime
import time
class config(object):
'''
config.xml
'''
def __init__(self, configfile):
'''
configfile:config files
'''
dom = parse(configfile)
self.config_element = dom.getElementsByTagName("config")[0]
def getSrcDir(self):
'''
return the <srcdir> element value of self.config_element
'''
srcDir = self.config_element.getElementsByTagName("srcdir")[0]
return self.getText(srcDir.childNodes)
def getDestDir(self):
'''
return the <destdir> element value of self.config_element
'''
destDir = self.config_element.getElementsByTagName("destdir")[0]
return self.getText(destDir.childNodes)
def getNotIncludeDirs(self):
'''
return a list, it's the <dir> element values of self_config_element
'''
notinclude_dirs = self.config_element.getElementsByTagName("dir")
dirList = []
for node in notinclude_dirs:
dir = self.getText(node.childNodes)
if dir != '':
dirList.append(dir)
return dirList
def getNotIncludeFiles(self):
'''
return a list, it's the <file> element values of self.config_element
'''
notinclude_files = self.config_element.getElementsByTagName("file")
fileList = []
for node in notinclude_files:
file = self.getText(node.childNodes)
if file != '':
fileList.append(file)
return fileList
def getText(self, nodeList):
'''
return the text value of the nodeList node
'''
rc = ''
for node in nodeList:
if node.nodeType == node.TEXT_NODE:
rc = rc + node.data
return rc
def getInitTime(self):
'''
return a datetime object,it's the <inittime> element value of self.config_element
'''
initTime = self.config_element.getElementsByTagName("inittime")[0]
timeStr = self.getText(initTime.childNodes)
dt = datetime.datetime.strptime(timeStr, "%Y-%m-%d %H:%M:%S")
fdt = time.mktime(dt.utctimetuple())
return fdt
def getWinRarDir(self):
'''
return the value of <rardir> element value
'''
rardir = self.config_element.getElementsByTagName('rardir')[0]
return self.getText(rardir.childNodes)
if __name__ == '__main__':
c = config('config.xml')
home = c.getSrcDir()
print('home is ', home)
dest = c.getDestDir()
print('dest is ', dest)
dirlist = c.getNotIncludeDirs()
print('not include directory is:')
for n in dirlist:
print(n)
filelist = c.getNotIncludeFiles()
print('not include files is:')
for n in filelist:
print(n)
inittime = c.getInitTime()
print('inittime is', inittime)
rardir = c.getWinRarDir()
print(rardir)

下面是程序的主體:
fetchfile.py
復(fù)制代碼 代碼如下:

'''
Created on Mar 3, 2009
@author: alex cheng
'''
from config import config
from os import chdir, listdir, makedirs, system, walk, remove, rmdir, unlink, \
removedirs, stat, getcwd
from os.path import abspath, isfile, isdir, join as join_path, exists
from shutil import copy2
from sys import path
import datetime
import re
import time
def getdestdir(dir):
'''
return the dest directory name;
it's named by date,for example 20090101; if 20090101 has exist the return 20090101(1),if 20090101(1) has exist also,
then return 20090101(2), and then...
'''
today = datetime.datetime.today()
strtoday = today.strftime('%Y%m%d')
dr = join_path(dir, strtoday)
tmp = dr
index = 0
while isdir(tmp):
tmp = dr
index = index + 1
tmp = tmp + '(' + '%d' % index + ')'
return tmp
def fetchFiles(srcdir, destdir, ignoredirs, ignorefiles, lasttime=time.mktime(datetime.datetime(2000, 1, 1).utctimetuple())):
'''
fetch files from srcdir(source directory) to destdir(dest directory) ignore the notcopydires(the ignore directory list)
and notcopyfiles(the ignore file list), and the file and directory's modify time after the lasttime
'''
chdir(srcdir) # change the current directory to the srcdir
dirs = listdir('.') # get all files and directorys in srcdir, but ignore the "." and ".."
dirlist = [] # save all directorys in srcdir
for n in dirs:
if isdir(n):
dirlist.append(n)
for subdir in dirlist:
exist = False
for ignoredir in ignoredirs:
if join_path(srcdir, subdir) == ignoredir:
exist = True
break
if exist:
continue
fetchFiles(join_path(srcdir, subdir), join_path(destdir, subdir), ignoredirs, ignorefiles, lasttime)
copyfiles(srcdir, destdir, ignorefiles, lasttime)
def copyfiles(srcdir, destdir, ignorefiles, lasttime):
'''
copy the files from srcdir(source directory) to destdir(dest directory, if dest directory not exist then create is)
ignore the notcopyfiles(the ignore file list) and the file's modify time must after lasttime
'''
chdir(srcdir)
files = filter(isfile, listdir('.'))
for file in files:
if isdir(file): # ignore the directory
continue
lastmodify = stat(file).st_mtime
if lastmodify < lasttime:
continue
exist = False
for ignorefile in ignorefiles:
if join_path(srcdir, file) == ignorefile:
exist = True
if not exist:
if isdir(destdir) is False:
try:
makedirs(destdir)
print('success create directory:', destdir)
except:
raise Exception('failed create directory: ' + destdir)
try:
copy2(file, join_path(destdir, file))
print('success copy file from', join_path(srcdir, file), 'to', join_path(destdir, file))
except:
raise Exception('failed copy file from ' + join_path(srcdir, file) + ' to ' + join_path(destdir, file))

def tarfiles(dir, todir, winrardir, tarfilename):
'''
tar all files in dir(a directory) to todir(dest directory) and the tar file named tarfilename
'''
if isdir(dir) is False:
print('the directory', dir, 'not exist')
return
chdir(dir)
commond = '\"' + winrardir + '\\rar.exe\" a -r ' + todir + '\\' + tarfilename + ' *.*'
print(commond)
if system(commond) == 0:
print('success tar files')
else:
print('failed tar files')

def removeDir(dir_file, currentdir):
'''
delete the dir_file
'''
if isdir(currentdir) is False:
print()
return
chdir(currentdir)
if not exists(dir_file):
return
if isdir(dir_file):
for root, dirs, files in walk(dir_file, topdown=False):
for name in files:
remove(join_path(root, name))
for name in dirs:
rmdir(join_path(root, name))
rmdir(dir_file) # remove the main dir
else:
unlink(dir_file)
return
def getlasttime():
'''
get last modify time from txt files
'''
try:
mypath = abspath(path[0]) #get current path
file = join_path(mypath, 'C_UPGRADETIME.txt')
if isfile(file) is False:
return 0
f = open(join_path(mypath, 'C_UPGRADETIME.txt'), 'r')
lines = f.readlines()
if len(lines) == 0:
return 0
line = lines[ - 1]

dt = datetime.datetime.strptime(line, "%Y-%m-%d %H:%M:%S")
lasttime = time.mktime(dt.utctimetuple())
f.close()
return lasttime
except:
print('failed to get last modify time from txt file')
return 0
def registtime():
nowstr = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
nowfloat = time.time()
mypath = abspath(path[0]) # get current path
f = open(join_path(mypath, 'C_UPGRADETIME.txt'), 'a')
f.write('\n' + nowstr)
f.close()
def main():
c = config('config.xml')
home = c.getSrcDir()
dest = c.getDestDir()
ignoreDirs = c.getNotIncludeDirs()
ignoreFiles = c.getNotIncludeFiles()
winRarDir = c.getWinRarDir()

dest = getdestdir(dest)# get current dest directory

print ('copy all files to the temp directory ignore last fetch time')
fetchFiles(home, join_path(dest, 'temp'), ignoreDirs, ignoreFiles)

print('tar the all files')
tarfiles(join_path(dest, 'temp'), dest, winRarDir, 'CargillUpdate_ALL.rar')

print('program sleep 20 seconds to finish the tar thread')
time.sleep(20)

print('remove the temp directory...')
removeDir(join_path(dest, 'temp'), dest)
print('success remove the temp directory')
lasttime = getlasttime() # get last modify time from txt files
if lasttime == 0:
lasttime = c.getInitTime()
print ('copy all files to the temp2 directory last modify time after last fetch time')
fetchFiles(home, join_path(dest, 'temp2'), ignoreDirs, ignoreFiles, lasttime)

print('tar the all files')
tarfiles(join_path(dest, 'temp2'), dest, winRarDir, 'CargillUpdate.rar')
print('program sleep 20 seconds to finish the tar thread')
time.sleep(20)

print('remove the temp2 directory...')
removeDir(join_path(dest, 'temp2'), dest)
print('success remove the temp2 directory')

registtime() # regist current time
if __name__ == '__main__':
main()

相關(guān)文章

  • selenium+python自動(dòng)化測(cè)試之多窗口切換

    selenium+python自動(dòng)化測(cè)試之多窗口切換

    這篇文章主要介紹了selenium+python自動(dòng)化測(cè)試之多窗口切換,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • python+selenium+Chrome options參數(shù)的使用

    python+selenium+Chrome options參數(shù)的使用

    這篇文章主要介紹了python+selenium+Chrome options參數(shù)的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • Python生成驗(yàn)證碼實(shí)例

    Python生成驗(yàn)證碼實(shí)例

    這篇文章主要介紹了Python生成驗(yàn)證碼的方法,具有很好的實(shí)用價(jià)值,代碼結(jié)構(gòu)清晰易懂,需要的朋友可以參考下
    2014-08-08
  • 上帝為你開(kāi)了一扇窗之Tkinter常用函數(shù)詳解

    上帝為你開(kāi)了一扇窗之Tkinter常用函數(shù)詳解

    構(gòu)思了很長(zhǎng)一段時(shí)間,總感覺(jué)不夠有趣,于是打算出一個(gè)完整的系列,讓大家一起感受python的樂(lè)趣.這個(gè)系列著重以系統(tǒng)庫(kù)中的tkinter為中心來(lái)圍繞進(jìn)行編寫(xiě).因此我們的第一步是導(dǎo)入模塊, 第一節(jié)就來(lái)為大家建立一個(gè)窗口 ,需要的朋友可以參考下
    2021-06-06
  • Python通過(guò)tkinter實(shí)現(xiàn)百度搜索的示例代碼

    Python通過(guò)tkinter實(shí)現(xiàn)百度搜索的示例代碼

    這篇文章主要介紹了Python通過(guò)tkinter實(shí)現(xiàn)百度搜索的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • python庫(kù)pydantic的簡(jiǎn)易入門(mén)教程

    python庫(kù)pydantic的簡(jiǎn)易入門(mén)教程

    pydantic庫(kù)是一種常用的用于數(shù)據(jù)接口schema定義與檢查的庫(kù),通過(guò)pydantic庫(kù),我們可以更為規(guī)范地定義和使用數(shù)據(jù)接口,下面這篇文章主要給大家介紹了關(guān)于python庫(kù)pydantic的簡(jiǎn)易入門(mén)教程,需要的朋友可以參考下
    2022-03-03
  • 安裝PyTorch的詳細(xì)過(guò)程記錄

    安裝PyTorch的詳細(xì)過(guò)程記錄

    PyTorch是一個(gè)基于Python的科學(xué)計(jì)算框架,用于進(jìn)行深度學(xué)習(xí)相關(guān)研究,下面這篇文章主要給大家介紹了關(guān)于安裝PyTorch的詳細(xì)過(guò)程,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2022-03-03
  • python opencv實(shí)現(xiàn)切變換 不裁減圖片

    python opencv實(shí)現(xiàn)切變換 不裁減圖片

    這篇文章主要為大家詳細(xì)介紹了python opencv實(shí)現(xiàn)切變換,不裁減圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • python版本單鏈表實(shí)現(xiàn)代碼

    python版本單鏈表實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了python版本單鏈表實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-09-09
  • Python 實(shí)現(xiàn)日志同時(shí)輸出到屏幕和文件

    Python 實(shí)現(xiàn)日志同時(shí)輸出到屏幕和文件

    這篇文章主要介紹了Python 實(shí)現(xiàn)日志同時(shí)輸出到屏幕和文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-02-02

最新評(píng)論