python復制多層目錄下的文件至其他盤符對應的目錄中


一、需求

app打包需要打入一些H5進去,以便更快的加載頁面。這些H5文件是散落在各個文件夾中的【如下列所示】,偶爾各個文件夾還需新增文件,每次新增一個文件,需要改動jenkins上job腳本,比較麻煩,所以換一種思路來解決這個問題。

tmp/
├── c
│   ├── cmd.js.d
│   ├── TZT2.0.js.d
│   ├── TZT.js.d
│   └── TZT\346\226\207\346\241\243\350\257\264\346\230\216.json.d
├── c_modules
│   ├── config.js.d
│   ├── css
│   │   ├── base-min2.0.css.d
│   │   ├── base-min.css.d
│   │   ├── base-min-white.css.d
│   │   ├── images
│   │   │   └── arrow.png.d
│   │   ├── jskj-elementUI.css.d
│   │   ├── scroller.css.d
│   │   ├── skin
│   │   │   └── bg.css.d
│   │   └── skin01
│   │       └── bg.css.d
│   ├── debuggap.js.d
│   ├── fastclick.js.d
│   ├── heatmap.min.js.d
│   ├── jquery-1.7.1.min.js.d
│   ├── jquery.min.js.d
│   ├── loadData.js.d
│   ├── loadSel.js.d
│   ├── onscroll.js.d
│   ├── pop
│   │   ├── images
│   │   │   └── icon_closeBtn.png.d
│   │   ├── pop.css.d
│   │   ├── pop.js.d
│   │   ├── pop-zdycx.css.d
│   │   └── pop-zdycx.js.d
│   ├── proint.js.d
│   ├── sea.js.d
│   └── sensorsdata.min.js.d
├── config
│   └── index.js.d
├── dist
│   ├── src
│   │   ├── common
│   │   │   ├── iconClick.js.d
│   │   │   └── md5.js.d
│   │   ├── home
│   │   │   ├── app.js.d
│   │   │   ├── images
│   │   │   │   ├── close.png.d
│   │   │   │   ├── default.png.d
│   │   │   │   ├── down.png.d
│   │   │   │   ├── dxsq.png.d
│   │   │   │   ├── fxng.png.d
│   │   │   │   ├── gpcc.png.d
│   │   │   │   ├── gpkh.png.d
│   │   │   │   ├── guide1.png.d
│   │   │   │   ├── guide2.png.d
│   │   │   │   ├── guide3.png.d
│   │   │   │   ├── guide4.png.d
│   │   │   │   ├── header_bg.png.d
│   │   │   │   ├── jftoast.png.d
│   │   │   │   ├── mid_xgsgts.png.d
│   │   │   │   ├── myzc.png.d
│   │   │   │   ├── new.png.d
│   │   │   │   ├── news.png.d
│   │   │   │   ├── nodata.png.d
│   │   │   │   ├── oper_activity_pop.png.d
│   │   │   │   ├── register_tel.png.d
│   │   │   │   ├── right.png.d
│   │   │   │   ├── rmzt_one.png.d
│   │   │   │   ├── rmzt_three.png.d
│   │   │   │   ├── rmzt_two.png.d
│   │   │   │   ├── selected.png.d
│   │   │   │   ├── select.png.d
│   │   │   │   ├── shadow.png.d
│   │   │   │   ├── tougu_bg.png.d
│   │   │   │   ├── up.png.d
│   │   │   │   ├── user.png.d
│   │   │   │   ├── voice.gif.d
│   │   │   │   ├── yszb.png.d
│   │   │   │   └── ywbl.png.d
│   │   │   ├── index.html.d
│   │   │   ├── js
│   │   │   │   ├── home_index.js.d
│   │   │   │   └── Sortable.js.d
│   │   │   ├── routes
│   │   │   │   └── routes.js.d
│   │   │   ├── skin01
│   │   │   │   └── home_index.css.d
│   │   │   └── template
│   │   │       └── home_index.html.d
│   │   ├── mine
│   │   │   ├── api
│   │   │   │   └── function.js.d
│   │   │   ├── app.js.d
│   │   │   ├── css
│   │   │   │   └── index.css.d
│   │   │   ├── img
│   │   │   │   ├── arrow_account.png.d
│   │   │   │   ├── cao.png.d
│   │   │   │   ├── common_used.png.d
│   │   │   │   ├── crown.png.d
│   │   │   │   ├── detail.png.d
│   │   │   │   ├── dui.png.d
│   │   │   │   ├── flow.png.d
│   │   │   │   ├── guide1.png.d
│   │   │   │   ├── guide2.png.d
│   │   │   │   ├── guide3.png.d
│   │   │   │   ├── guide4.png.d
│   │   │   │   ├── guide5.png.d
│   │   │   │   ├── hide_assets.png.d
│   │   │   │   ├── jcsc.png.d
│   │   │   │   ├── ka.png.d
│   │   │   │   ├── not_login.png.d
│   │   │   │   ├── show_assets.png.d
│   │   │   │   ├── sign.png.d
│   │   │   │   ├── transfer_accounts.png.d
│   │   │   │   ├── tui.png.d
│   │   │   │   ├── vip_arrow.png.d
│   │   │   │   ├── vip.png.d
│   │   │   │   ├── wei.png.d
│   │   │   │   └── wen.png.d
│   │   │   ├── index.html.d
│   │   │   ├── js
│   │   │   │   └── index.js.d
│   │   │   ├── routes
│   │   │   │   └── routes.js.d
│   │   │   ├── skin
│   │   │   │   └── index.css.d
│   │   │   ├── skin01
│   │   │   │   └── index.css.d
│   │   │   └── template
│   │   │       └── index.html.d
│   │   └── public-images
│   │       ├── cccb.png.d
│   │       ├── dkxh.png.d
│   │       ├── dpjl.png.d
│   │       ├── dtzq.png.d
│   │       ├── dxsq.png.d
│   │       ├── dzjy.png.d
│   │       ├── fhsp.png.d
│   │       ├── fxng.png.d
│   │       ├── fxspg.png.d
│   │       ├── gdrs.png.d
│   │       ├── ggcg.png.d
│   │       ├── ggtjy.png.d
│   │       ├── ggt.png.d
│   │       ├── gpcc.png.d
│   │       ├── gpkh.png.d
│   │       ├── gpqq.png.d
│   │       ├── gsrl.png.d
│   │       ├── gszx.png.d
│   │       ├── gznhg.png.d
│   │       ├── jcsc.png.d
│   │       ├── jfsc.png.d
│   │       ├── jgdy.png.d
│   │       ├── jjzt.png.d
│   │       ├── lcsc_new.png.d
│   │       ├── lhb.png.d
│   │       ├── mfjg.png.d
│   │       ├── myzc.png.d
│   │       ├── nrzt.png.d
│   │       ├── qbfw.png.d
│   │       ├── rmzt.png.d
│   │       ├── rzrq.png.d
│   │       ├── sczd.png.d
│   │       ├── sjjj.png.d
│   │       ├── ssnc.png.d
│   │       ├── tfp.png.d
│   │       ├── tgsq.png.d
│   │       ├── xsjj.png.d
│   │       ├── xskx.png.d
│   │       ├── xyjy.png.d
│   │       ├── yjyg.png.d
│   │       ├── ywbl_old.png.d
│   │       ├── ywbl.png.d
│   │       ├── yzzz.png.d
│   │       ├── zlcc.png.d
│   │       ├── znxg.png.d
│   │       ├── znyj.png.d
│   │       └── znzg.png.d
│   └── static
│       ├── css
│       │   ├── base.css.d
│       │   ├── base-min.css.d
│       │   ├── base-min-sea-vue.css.d
│       │   ├── mobiscroll.custom-2.6.2.min.css.d
│       │   └── swiper.css.d
│       ├── jquery
│       │   ├── jquery-1.11.3.min.js.d
│       │   ├── jquery-1.8.3.min.js.d
│       │   └── jquery-range.js.d
│       ├── lib
│       │   ├── action.js.d
│       │   ├── flexible.js.d
│       │   ├── mobiscroll.custom-2.6.2.min.js.d
│       │   └── swiper.js.d
│       ├── sea
│       │   ├── sea.js.d
│       │   └── seajs-text.js.d
│       └── vue
│           ├── vue.js.d
│           ├── vue-router.min.js.d
│           ├── vue-scroller.min.js.d
│           └── vue-scroller.min.js.map.d
├── frontend_7_06_js_test_0303.7z
└── jy
    └── ggqq
        ├── function
        │   └── function.js.d
        ├── ggqq_index.html.d
        ├── image
        │   ├── add.png.d
        │   ├── delete.png.d
        │   ├── drcj.png.d
        │   ├── fall.png.d
        │   ├── ggqq_chicang.png.d
        │   ├── ggqq_cljy.png.d
        │   ├── ggqq_index_b.png.d
        │   ├── ggqq_index.png.d
        │   ├── ggqq_kcwt.png.d
        │   ├── ggqq_ksxd.png.d
        │   ├── ggqq_minus.png.d
        │   ├── ggqq_nologin.png.d
        │   ├── ggqq_plus.png.d
        │   ├── ggqq_sybg.png.d
        │   ├── ggqq_xq.png.d
        │   ├── gou.png.d
        │   ├── kcwt.png.d
        │   ├── loading.gif.d
        │   ├── minus.png.d
        │   ├── mymoney.png.d
        │   ├── notselect.png.d
        │   ├── others@2x.png.d
        │   ├── reset.png.d
        │   ├── right2.png.d
        │   ├── right.png.d
        │   ├── rise.png.d
        │   ├── select.png.d
        │   ├── shoppingcar.png.d
        │   └── zhcl@2x.png.d
        ├── js
        │   └── ggqq_index.js.d
        ├── skin
        │   ├── bdq.css.d
        │   ├── chedan.css.d
        │   ├── ggqq_bdsx.css.d
        │   ├── ggqq_chicang.css.d
        │   ├── ggqq_edcx.css.d
        │   ├── ggqq_hylist.css.d
        │   ├── ggqq_hysx.css.d
        │   ├── ggqq_index.css.d
        │   ├── ggqq_notice.css.d
        │   ├── ggqq_qindex.css.d
        │   ├── ggqq_xq.css.d
        │   ├── ggqq_xycx.css.d
        │   ├── ggqq_xyxq.css.d
        │   ├── jsd.css.d
        │   ├── login_mesg.css.d
        │   ├── qchicang.css.d
        │   ├── revisepassword.css.d
        │   └── zjcc.css.d
        └── skin01
            ├── bdq.css.d
            ├── ggqq_bankcr.css.d
            ├── ggqq_bdsx.css.d
            ├── ggqq_chicang.css.d
            ├── ggqq_edcx.css.d
            ├── ggqq_hylist.css.d
            ├── ggqq_hysx.css.d
            ├── ggqq_index.css.d
            ├── ggqq_notice.css.d
            ├── ggqq_qindex.css.d
            ├── ggqq_xq.css.d
            ├── ggqq_xycx.css.d
            ├── ggqq_xyxq.css.d
            ├── jsd.css.d
            ├── login_mesg.css.d
            ├── revisepassword.css.d
            └── zjcc.css.d
View Code

二、思路

讓前端開發把需要打入app的H5文件放入D:\scripts\src目錄上  --->  讀取該目錄下文件的絕對路徑  --->  在workspace中尋找對應的文件 ---> 並將這些文件復制到新的目錄中,進行7za打包

python代碼如下:

 1 #!/usr/bin/env python3
 2 #-*- coding:GBK -*-
 3 # author by Michael Ho
 4 # contact:rui.he@geekthings.com.cn
 5 import os, shutil
 6 
 7 def copy_app_H5_file(x_root, x_dir, src_dir, dst_dir):
 8     if os.path.exists(dst_dir):
 9         shutil.rmtree(dst_dir)
10     for root, dirs, files in os.walk(x_root):
11         for d_name in dirs:
12             d_name = os.path.join(root, d_name).rstrip()
13             d_dir = d_name.replace(x_dir, dst_dir)
14             if not os.path.exists(d_dir):
15                 os.makedirs(d_dir)
16         for f_name in files:
17             f_name = os.path.join(root, f_name).rstrip()            
18             if(os.path.splitext(f_name)[1] == ".d"):
19                 s_file = f_name.replace(x_dir, src_dir)
20                 d_file = f_name.replace(x_dir, dst_dir)
21                 shutil.copyfile(s_file, d_file)
22                 # 判斷復制是否成功
23                 if(s_file.replace(src_dir, "") == d_file.replace(dst_dir, "")):
24                     print(d_file + "->" + "拷貝成功")
25                 else:
26                     print("拷貝過程發生錯誤,請檢查...")
27 
28 if __name__ == '__main__':
29     # 上傳的小包根目錄,默認是d:\scripts,不要去動它!!!
30     x_root = "d:\\scripts"
31     
32     # 小包目錄,默認是src,需要把小包的文件放在一個叫src目錄里面!!!
33     x_dir = "d:\\scripts\\src"
34     
35     # 從gitlab上獲取的目錄
36     src_dir = "d:\\Jenkins\\frontend_encrypt"
37 
38     # 需要復制到目標目錄,一般對其目錄進行打包
39     dst_dir = "d:\\Jenkins\\H5_APP"
40 
41     copy_app_H5_file(x_root, x_dir, src_dir, dst_dir)

三、說明

1.在Jenkins客戶端在Windows上,python編碼格式要設定成    # -*- coding:GBK -*-  (筆者當時寫的是 # -*- coding: utf-8 -*-)不然Jenkins會報以下錯誤

SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xbf in position 0: invalid start byte

2.python調系統級的接口不算太友好,個人認為沒有批處理方便。如果整個文件夾復制的話,還是 xcopy  src_dir dst_dir /s /e /f方便,所以Jenkins上打包的腳本,筆者為了打包速度,Windows打包機采用batch+python【以上個人見解、水平有限】


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM