Python automation-extract modified files from Windows development environment and compress them to the specified FTP

Source: Internet
Author: User

In daily work, there are often projects that need to be updated after modification. It is very troublesome to pack all war package updates and write a script to achieve the following:

Extract files modified after a specified date

Filter some directories and files

Pack extracted files

FTP to the specified server

Previously, I wrote one with vbs and VB, Which is inefficient. It is very efficient to write data in Java. If you want to modify the data, you need to use eclipse. Finally, you can use the python + doscommand to solve the problem of fast modification and efficiency.

Eclipse project directory version:

import osimport shutilimport tarfilefrom ftplib import FTPapp_name     = "lfpt_hb"project_home = "D:\java\project_hf"update_home  = "E:\PythonUpdate"time_modify  = "4-26-2013"  #format m-d-ypath_src_web = r"%s\%s\WebContent" %(project_home, app_name)path_src_cls = r"%s\%s\build\classes" %(project_home, app_name)path_src_lib = r"%s\%s\WebContent\WEB-INF\lib" %(project_home, app_name)path_dst_web = r"%s\%s" %(update_home, app_name)path_dst_cls = r"%s\%s\WEB-INF\classes" %(update_home, app_name)path_dst_lib = r"%s\%s\WEB-INF\lib" %(update_home, app_name)file_zip     = r"%s\%s_%s.tar.gz" %(update_home, app_name, time_modify)#set exclude file for xcopy /exclude:fileexclude_all = "/EXCLUDE:update_client_exclude_all.txt"exclude_lib = "/EXCLUDE:update_client_exclude_lib.txt"# ftp settingsftp_server   = ""ftp_port     = "21"ftp_user     = "update"ftp_password = "update"def copy_web_files():    cmd_xcopy = 'xcopy %s %s /I /Y /S /D:%s %s' %(path_src_web, path_dst_web, time_modify, exclude_all)    print(cmd_xcopy)    os.system(cmd_xcopy)    print("copy_web_files end...")def copy_jar_files():    cmd_xcopy = "xcopy %s %s /I /Y /S %s" %(path_src_lib, path_dst_lib, exclude_lib)    print(cmd_xcopy)    os.system(cmd_xcopy)def copy_class_files():    if not os.path.exists(path_dst_cls):        cmd_xcopy = "xcopy %s %s /I /Y /S /D:%s %s" %(path_src_cls, path_dst_cls, time_modify, exclude_all)        print(cmd_xcopy)        os.system(cmd_xcopy)        print("copy_web_files end...")def tar_files():    print("taring files...")    tar =, "w:gz")    tar.add(path_dst_web, "")    tar.close()def ftp_stor_files():    cmd_stor = "STOR %s" %(os.path.split(file_zip)[1])    print(cmd_stor)    ftp = FTP(ftp_server, ftp_user, ftp_password)    ftp.getwelcome()    ftp.storbinary(cmd_stor, open(file_zip, "rb"), 1024)    ftp.close()    #ftp.quit()def clear():    cmd_rmdir = "rmdir /S /Q %s" %(path_dst_web)    cmd_del = "del /S /Q %s" %(file_zip)    print cmd_rmdir    print cmd_del    os.system(cmd_rmdir)    os.system(cmd_del)    if __name__ == "__main__":    copy_web_files()    #copy_jar_files()    copy_class_files()    tar_files()    ftp_stor_files()    clear()    print("done, python is great!")

Version of the maven directory structure:

#! /Usr/bin/ENV Python #-*-coding: UTF-8-*-# @ author # @ version 2013-05-28import osimport shutilimport tarfilefrom ftplib import FTP # The following parts may need to be modified app_name = "EOA-web" project_home = "D: \ Java \ workspace \ Project-SVN \ EOA-parent "update_home =" F: \ pythonupdate "time_modify =" 5-27-2013 "# format m-d-ypath_src_web = r" % s \ SRC \ main \ webapp "% (project_home, app_name) path_src_cls = r "% s \ tar Get \ Classes "% (project_home, app_name) path_src_lib = r" % s \ target \ EOA-web \ WEB-INF \ Lib "% (project_home, app_name) path_dst_web = r "% s \ % s" % (update_home, app_name) path_dst_cls = r "% s \ WEB-INF \ Classes" % (update_home, app_name) path_dst_lib = r "% s \ WEB-INF \ Lib" % (update_home, app_name) file_zip = r "% s \ s_s_%s.tar.gz" % (update_home, app_name, time_modify) # Set exclude file for xcopy/exclud E: fileexclude_all = "/exclude: update_client_exclude_all.txt" exclude_lib = "/exclude: update_client_exclude_lib.txt "# ftp settingsftp_server =" "ftp_port =" 21 "ftp_user =" admin "ftp_password =" admin "def copy_web_files (): export _xcopy = 'xcopy % S/I/y/S/D: % S % s' % (path_src_web, path_dst_web, time_modify, exclude_all) print (export _xcopy) OS. system (pai_xcopy) print ("copy_web_files end...") Def copy_jar_files (): Export _xcopy = "xcopy % S/I/y/S % s" % (path_src_lib, path_dst_lib, exclude_lib) print (export _xcopy) OS. system (cmd_xcopy) def copy_class_files (): If not OS. path. exists (path_dst_cls): pai_xcopy = "xcopy % S/I/y/S/D: % S % s" % (path_src_cls, path_dst_cls, time_modify, exclude_all) print (pai_xcopy) OS. system (pai_xcopy) print ("copy_web_files end... ") def tar_files (): Print (" Tarin G files... ") tar = tarfile. open (file_zip, "W: Gz") tar. add (path_dst_web, "") tar. close () def ftp_stor_files (): pai_stor = "stor % s" % (OS. path. split (file_zip) [1]) print (pai_stor) FTP = FTP (ftp_server, ftp_user, ftp_password) FTP. getwelcome () FTP. storbinary (pai_stor, open (file_zip, "rb"), 1024) FTP. close () # ftp. quit () def clear (): pai_rmdir = "rmdir/S/Q % s" % (path_dst_web) pai_del = "del/S/Q % s" % (File_zip) print Export _rmdir print Export _del OS. system (cmd_rmdir) OS. system (cmd_del) # Main running program, you may need to modify if _ name _ = "_ main _": copy_web_files () # copy_jar_files () copy_class_files () tar_files () ftp_stor_files () # clear () print ("done, python is great! ")

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: 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.