Python Extracts the file applet _python

Source: Internet
Author: User
Tags create directory current time gettext rar
These files were previously extracted using a colleague's batch file; Not quite handy, just recently learning some python, all on their own to write a Python extract files applet;
1. Principle
The principle of extracting files is very simple, that is, to a specified directory, to find the last modified time than a given time files, and then copy them to the target directory, the structure of the target directory must be the same as the original directory, so that the project staff can be directly covered by the entire directory;
2. Realize
For the general purpose of the program, I have defined the following configuration file
Config.xml
Copy Code code as follows:

<?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>

which
<srcdir&gt: The original directory, that is, our Tomcat publishing directory;
<destdir&gt: Files copied to the target directory;
<notinclude&gt: The folders and files that need to be ignored, the content that needs to be ignored is defined in its child nodes, which is not explained here;
<inittime&gt: This is the initialization of the need to extract the point of time, after this will be extracted, here need to explain, later in use, I added a function, is that every time the extraction will automatically record the extraction time to a text file c_ In UPGRADETIME.txt, this eliminates the annoyance of setting this value every time, only if the c_upgradetime.txt is empty or does not exist, the value is used;
<rardir>:rar the address of the compression program;
The following is a class that reads a configuration file:
config.py
Copy Code code as follows:

'''
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 <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 <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 <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 the files is: ')
For N in FileList:
Print (n)
Inittime = C.getinittime ()
Print (' Inittime is ', inittime)
Rardir = C.getwinrardir ()
Print (Rardir)

The following is the body of the program:
fetchfile.py
Copy Code code as follows:

'''
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,
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 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 "."
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 (' 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, the 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 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 ' txt files
If Lasttime = 0:
Lasttime = C.getinittime ()
Print (' Copy all Files to "Temp2 directory last Modify
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 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 ()
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.