目录
一、需求:
二、python拷贝分析
1、需要的库,及源路径、目标路径定义
2、定义的拷贝数组
3、自定义拷贝函数
1) 如果目标路径不存在时,先创建目标路径
2)遍历元组数组中的文件
3)如果源文件或目录不存在时,创建目标路径
4)split源路径、目标路径
5)若为源文件为目录,则将目录子文件夹,整体拷贝到目标路径中
6)若file为文件时,
4、自定义拷贝函数
5、主函数中调用
一、需求:
将文件和文件夹按照一定规则拷贝到另一个位置
规则如下:
BuildDir = "C:\\build2" //目标文件夹
SourceDir=r"D:\biancheng\xnw2.0\xnw" //源文件夹
# (from, to)
# from: 以/结尾为目录,其他为文件
# to: 空为BuildDir , 非空为BuildDir /to
('xnwmath.ico', 'xnwmath.ico'),
表示,将原路径下的xnwmath.ico 移动到目标文件夹下同名文件
('bin/logo.png', 'xnwlive/'),
表示,将logo.png, 拷贝到目标路径下xnwlive目录中
('bin/themes/', 'xnwlive/'),
表示,将bin/下,themes这个文件夹包括所有子文件 全部拷贝到目标路径下xnwlive文件夹中
('', 'xnwlive'),
表示,在目标路径下,新建xnwlive目录
('', 'xnwlive/intlive'),
表示,在目标路径下,新建xnwlive/intlive 目录
('bin/cef/', 'xnwlive/')
表示,将cef文件夹,整体拷贝到目标路径的xnwlive下
('bin\cef\**', 'xnwlive'),
两个**,表示将cef下的所有文件和子文件夹统统拷贝到目录路径的xnwlive下
('bin\cef\*', 'xnwlive'),
一个*,表示将cef下的所有文件拷贝到目录路径的xnwlive下,不拷贝子文件夹
二、python拷贝分析
1、需要的库,及源路径、目标路径定义
这里注意: 路径名
import os
import shutil
BuildDir = "C:\\build2"
SourceDir=r"D:\biancheng\xnw2.0\xnw"2、定义的拷贝数组
FILES = [
    ('StartExe/StartExe/Release/StartExe.exe', '校内外.exe'),
    ('bin/校内外动态数学实验.exe', '校内外动态数学实验.exe'),
    ('xnw.ico', 'xnw.ico'),
    ('xnwmath.ico', 'xnwmath.ico'),
    ('', 'xnwlive'),
    #CEF
    ('bin\cef\**', 'xnwlive'),
    # xnwlive
    ('bin/themes/', 'xnwlive/'),
    ('bin/lang/', 'xnwlive/'),
    ('bin/dynamicMath/', 'xnwlive/'),
    ('xnwlib/bin/release/httpd.dll', 'xnwlive/'),
    ('xnwlib/bin/release/lavaagntex.dll', 'xnwlive/'),
    ('xnwlib/bin/release/cdnsapi.dll', 'xnwlive/'),
    ('xnwlib/bin/release/GsBase.dll', 'xnwlive/'),
    ('xnwlib/bin/release/blib.dll', 'xnwlive/'),
    ('bin/lava-def.ini', 'xnwlive/'),
    #('bin/ortherSet.ini', 'xnwlive/'),
    ('bin/render.exe', 'xnwlive/'),
    ('bin/xnw-live.exe', 'xnwlive/'),
    ('bin/updateVer.exe', 'xnwlive/'),
   
    ('bin/logo.png', 'xnwlive/'),
    ('bin/blackbk.png', 'xnwlive/'),
    #dll
    ('bin/avcodec-57.dll', 'xnwlive/'),
    ('bin/avdevice.dll', 'xnwlive/'),
    ('bin/avdevice-57.dll', 'xnwlive/'),
    ('bin/avfilter-6.dll', 'xnwlive/'),
    ('bin/avformat-57.dll', 'xnwlive/'),
    ('bin/avutil-55.dll', 'xnwlive/'),
    ('bin/base.dll', 'xnwlive/'),
    ('bin/clss.dll', 'xnwlive/'),
    ('bin/cos.dll', 'xnwlive/'),
    ('bin/cmps.dll', 'xnwlive/'),
    ('bin/d3dmodule.dll', 'xnwlive/'),
    ('bin/D3DX9_43.dll', 'xnwlive/'),
    ('bin/DXGIScreenCapture.dll', 'xnwlive/'),
    ('bin/engine.dll', 'xnwlive/'),
    ('bin/itrd.dll', 'xnwlive/'),
    ('bin/libwinpthread-1.dll', 'xnwlive/'),
    ('bin/libssl-1_1.dll', 'xnwlive/'),
    ('bin/libcrypto-1_1.dll', 'xnwlive/'),
    ('bin/libiconv-2.dll', 'xnwlive/'),
    ('bin/libfdk-aac-0.dll', 'xnwlive/'),
    ('bin/libstdc++-6.dll', 'xnwlive/'),
    ('bin/libgcc_s_dw2-1.dll', 'xnwlive/'),
    ('bin/libopenh264.dll', 'xnwlive/'),
    ('bin/libeay32.dll', 'xnwlive/'),
    ('bin/libx264-120.dll', 'xnwlive/'),
    ('bin/LSMediaCapture.dll', 'xnwlive/'),
    ('bin/msvcr120d.dll', 'xnwlive/'),
    ('bin/msvcr120.dll', 'xnwlive/'),
    ('bin/msvcp120.dll', 'xnwlive/'),
    ('bin/msvcp120d.dll', 'xnwlive/'),
    ('bin/msvcp140.dll', 'xnwlive/'),
    
    ('bin/NELivePlayer.dll', 'xnwlive/'),
 
    ('bin/pthreadGC-3.dll', 'xnwlive/'),
    ('bin/SDL2.dll', 'xnwlive/'),
    ('bin/swscale-4.dll', 'xnwlive/'),
    ('bin/ssleay32.dll', 'xnwlive/'), 
    ('bin/swresample-2.dll', 'xnwlive/'),
    ('bin/zlib1.dll', 'xnwlive/'),
    # xnwlive/amcap
    ('', 'xnwlive/amcap'),
    ('amcap/release/amcap.exe', 'xnwlive/amcap/'),
    ('bin/msvcr120.dll', 'xnwlive/amcap/'),
    ('bin/msvcp120.dll', 'xnwlive/amcap/'),
    # xnwlive/intlive
    ('', 'xnwlive/intlive'),
    ('intLive2.6/bin/layout/', 'xnwlive/intlive/'),
    ('intLive2.6/bin/lang/', 'xnwlive/intlive/'),
    ('intLive2.6/bin/nim_player/', 'xnwlive/intlive/'),
    ('intLive2.6/bin/nim_conf/', 'xnwlive/intlive/'),
    ('bin/msvcr120d.dll', 'xnwlive/intlive/nim_player/'),
    ('bin/msvcp120d.dll', 'xnwlive/intlive/nim_player/'),
    ('intLive2.6/bin/res/', 'xnwlive/intlive/'),
    ('intLive2.6/bin/themes/', 'xnwlive/intlive/'),
    ('intLive2.6/bin/intlive.exe', 'xnwlive/intlive/intlive.exe'),
    ('intLive2.6/bin/vcruntime140.dll', 'xnwlive/intlive/'),
    ('intLive2.6/bin/directui_license.txt', 'xnwlive/intlive/'),
    ('intLive2.6/bin/nim_data_client.dll', 'xnwlive/intlive/'),
    ('intLive2.6/bin/nim_audio_hook.dll', 'xnwlive/intlive/'),
    ('intLive2.6/bin/ucrtbase.dll', 'xnwlive/intlive/'),
    ('intLive2.6/bin/nrtc_audio_process.dll', 'xnwlive/intlive/'),
    ('intLive2.6/bin/vccorlib140.dll', 'xnwlive/intlive/'),
    ('intLive2.6/bin/duilib_license.txt', 'xnwlive/intlive/'),
    ('intLive2.6/bin/concrt140.dll', 'xnwlive/intlive/'),
    ('intLive2.6/bin/nertc_sdk.dll', 'xnwlive/intlive/'),
    ('intLive2.6/bin/image_ole.dll', 'xnwlive/intlive/'),
    ('intLive2.6/bin/libssl-1_1.dll', 'xnwlive/intlive/'),
    ('intLive2.6/bin/libcrypto-1_1.dll', 'xnwlive/intlive/'),
    ('intLive2.6/bin/msvcp140_1.dll', 'xnwlive/intlive/'),
    ('intLive2.6/bin/msvcp140_2.dll', 'xnwlive/intlive/'),
    ('intLive2.6/bin/nim_audio.dll', 'xnwlive/intlive/'),
    ('intLive2.6/bin/render.exe', 'xnwlive/intlive/'),
    ('intLive2.6/bin/msvcr120.dll', 'xnwlive/intlive/'),
    ('intLive2.6/bin/nim.dll', 'xnwlive/intlive/'),
    ('intLive2.6/bin/logo.png', 'xnwlive/intlive/'),
    ('intLive2.6/bin/logo2.png', 'xnwlive/intlive/'),
    ('intLive2.6/bin/msvcp120.dll', 'xnwlive/intlive/'),
    ('intLive2.6/bin/apisetschema.dll', 'xnwlive/intlive/'),
    ('intLive2.6/bin/protoopp.dll', 'xnwlive/intlive/'),
    ('intLive2.6/bin/blackbk.png', 'xnwlive/intlive/'),
    ('intLive2.6/bin/nim_tools_http.dll', 'xnwlive/intlive/'),
    ('intLive2.6/bin/nim_chatroom.dll', 'xnwlive/intlive/'),
    ('intLive2.6/bin/nrtc.dll', 'xnwlive/intlive/'),
    ('intLive2.6/bin/msvcp140.dll', 'xnwlive/intlive/'),
    ('intLive2.6\\bin\\api-ms-*.dll', 'xnwlive\\intlive\\'),
]
FILES_DEBUG = [
    ('bin/xnw-live.pdb', 'xnwlive/'),
    ('intLive2.6/bin/intlive.pdb','xnwlive/intlive/'),
]
3、自定义拷贝函数
1) 如果目标路径不存在时,先创建目标路径
    if not os.path.isdir(outdir):
        os.mkdir(outdir)2)遍历元组数组中的文件
    for file,dstpath in FILES + FILES_DEBUG:
        if file!='':
           file=os.path.join(SourceDir,file)3)如果源文件或目录不存在时,创建目标路径
('', 'xnwlive'),
        if not file:
            _file = os.path.join(outdir, dstpath)
            os.path.isdir(_file) or os.mkdir(_file)
            continue4)split源路径、目标路径
Split a pathname. Returns tuple "(head, tail)" where "tail" is everything after the final slash. Either part may be empty.
Slash是一个英文单词,可以表示斜线号
在计算机中,斜线号(/)也被称为正斜杠(forward slash),一般作为除法符号或者间隔符号使用,主要用于间隔使用。
        a_src = os.path.split(file)
        a_dst = os.path.split(dstpath)示例:
file 为 'D:\\biancheng\\xnw2.0\\xnw\\xnw.ico'
split后,
a_src[0] 'D:\\biancheng\\xnw2.0\\xnw'
a_src[1] 'xnw.ico'

dstpath 为 'xnw.ico'
split后

5)若为源文件为目录,则将目录子文件夹,整体拷贝到目标路径中
若split后,[1]为空,不存在,则表示file 为目录
如:
('bin/themes/', 'xnwlive/'),
将themes文件夹,整体拷贝到xnwlive 下
file : 'D:\\biancheng\\xnw2.0\\xnw\\bin/themes/'
split后

        if not a_src[1]: # 目录
            if not os.path.isdir(file):
                print('NOT EXIST: %s' % file)
                continue
            # 拷贝目录
            fname = os.path.split(a_src[0])[-1]
            _dst = os.path.join(dstpath, fname)
            shutil.copytree(file, os.path.join(outdir, _dst), dirs_exist_ok=True)
6)若file为文件时,
6.1)普通文件:
from:
D:\\biancheng\\xnw2.0\\xnw\\xnwlib/bin/release/httpd.dll
to: xnwlive\
            # 拷贝文件
            if a_dst[-1]: # 指定了文件
                _dst = dstpath
            else: # 只指定目录,使用源文件名
                _dst = os.path.join(dstpath, a_src[-1])
            shutil.copyfile(file, os.path.join(outdir, _dst))指定了新文件名
('StartExe/StartExe/Release/StartExe.exe', '校内外.exe'),

没有指定新文件名,
('xnwlib/bin/release/httpd.dll', 'xnwlive/')
D:\\biancheng\\xnw2.0\\xnw\\xnwlib/bin/release/httpd.dll
则,默认用原来的文件名一样
_dst = os.path.join(dstpath, a_src[-1])

6.2)** 两个星号,代表拷贝这个文件下的全部文件和子文件
            if file.find("**")!=-1:
                # xcopy 拷贝文件夹 xcopy c:\windows\file d:\file /e
                cmd="xcopy %s %s /e /y" % (file,os.path.join(outdir,dstpath))
                os.system(cmd)
                continue6.3)*一个星,代表只拷贝这个文件夹下的全部文件,不拷贝子文件夹
            if file.find("*") != -1: #如果含有统配符,用命令行copy
                # 只拷贝文件 ,不拷贝文件夹
                cmd="copy %s %s" % (file,os.path.join(outdir,dstpath))
                os.system(cmd)
                continue6.4)系统命令拷贝
这里使用系统命令进行拷贝,相关知识点如下
在Windows中,可以使用xcopy命令来拷贝文件夹。例如,如果你想将c:\windows\file文件夹复制到d:\file文件夹,可以使用以下命令:
xcopy c:\windows\file d:\file /e
其中,/e选项表示拷贝所有子目录和文件,包括空目录。 /y 表示直接覆盖 不提示
如果你想拷贝单个文件,可以使用copy命令。例如,如果你想将c:\file.txt文件复制到d:\目录下,可以使用以下命令:
copy c:\file.txt d:\xcopy D:\\biancheng\\xnw2.0\\xnw\\bin\\cef\\*.* C:\\build2\\Output\\xnwlive /e
4、自定义拷贝函数
虽然上面写了很多,但实际按上述规则创建的函数却很简洁,可以参考下面函数
def copy_files(outdir):
    if not os.path.isdir(outdir):
        os.mkdir(outdir)
    for file,dstpath in FILES + FILES_DEBUG:
        if file!='':
           file=os.path.join(SourceDir,file)
       
        # 建目录
        if not file:
            _file = os.path.join(outdir, dstpath)
            os.path.isdir(_file) or os.mkdir(_file)
            continue
        a_src = os.path.split(file)
        a_dst = os.path.split(dstpath)
        if not a_src[1]: # 目录
            if not os.path.isdir(file):
                print('NOT EXIST: %s' % file)
                continue
            # 拷贝目录
            fname = os.path.split(a_src[0])[-1]
            _dst = os.path.join(dstpath, fname)
            shutil.copytree(file, os.path.join(outdir, _dst), dirs_exist_ok=True)
        else: # 文件
            if file.find("**")!=-1:
                # xcopy 拷贝文件夹 xcopy c:\windows\file d:\file /e
                cmd="xcopy %s %s /e /y" % (file,os.path.join(outdir,dstpath))
                os.system(cmd)
                continue
            if file.find("*") != -1: #如果含有统配符,用命令行copy
                # 只拷贝文件 ,不拷贝文件夹
                cmd="copy %s %s" % (file,os.path.join(outdir,dstpath))
                os.system(cmd)
                continue
            if not os.path.isfile(file):
                print('NOT EXIST: %s' % file)
                continue            
            # 拷贝文件
            if a_dst[-1]: # 指定了文件
                _dst = dstpath
            else: # 只指定目录,使用源文件名
                _dst = os.path.join(dstpath, a_src[-1])
            shutil.copyfile(file, os.path.join(outdir, _dst))5、主函数中调用
if __name__ == '__main__':
    outdir = os.path.join(BuildDir, 'Output')
    print('===============================\ncollect files to %s ...' % outdir)
    copy_files(outdir)
    print("---end----")








![[蓝帽杯 2022 初赛]之Misc篇(NSSCTF)刷题记录(复现)⑨](https://img-blog.csdnimg.cn/0400354708144ba7b95f3d7560635eb8.png)









