python 开发 1 之 拷贝文件

news/2025/3/15 6:27:28/

目录

一、需求:

二、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 shutilBuildDir = "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)continue

4)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 = dstpathelse: # 只指定目录,使用源文件名_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 /ecmd="xcopy %s %s /e /y" % (file,os.path.join(outdir,dstpath))os.system(cmd)continue

6.3)*一个星,代表只拷贝这个文件夹下的全部文件,不拷贝子文件夹

            if file.find("*") != -1: #如果含有统配符,用命令行copy# 只拷贝文件 ,不拷贝文件夹cmd="copy %s %s" % (file,os.path.join(outdir,dstpath))os.system(cmd)continue

6.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)continuea_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 /ecmd="xcopy %s %s /e /y" % (file,os.path.join(outdir,dstpath))os.system(cmd)continueif file.find("*") != -1: #如果含有统配符,用命令行copy# 只拷贝文件 ,不拷贝文件夹cmd="copy %s %s" % (file,os.path.join(outdir,dstpath))os.system(cmd)continueif not os.path.isfile(file):print('NOT EXIST: %s' % file)continue            # 拷贝文件if a_dst[-1]: # 指定了文件_dst = dstpathelse: # 只指定目录,使用源文件名_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----")


http://www.ppmy.cn/news/61666.html

相关文章

[数据库系统] 二、表的基本操作(educoder)

目录 第一关:修改表名 一、知识点 ①查看所有表:show tables; ②查看表基本结构:DESCRIBE 表名; 或 简写为 desc 表名; ③查看创建表的语句:SHOW CREATE TABLE 二、educoder 第二关: 修改表结构-1 一、…

如何在Jetpack Compose中设置渐变背景

如何在Jetpack Compose中设置渐变背景 只需几步即可通过平滑渐变增强应用程序的用户界面 虽然它经常出现在网络前端的世界中,但渐变背景可以为您的移动应用程序增添专业和美观的触感,使其对您的用户更具吸引力。 第 1 步:创建渐变画笔 为…

Thingsboard开源物联网平台智慧农业实例快速部署教程(三)【源码部署及logo更换】

Thingsboard源码安装并更换logo 文章目录 Thingsboard源码安装并更换logo1. 源码拉取2. 导入项目3. 项目编译4. 项目启动5. 修改logo6. 部署(rpm deb方式) 1. 源码拉取 本教程按照3.4发行版本行进 git clone https://github.com/thingsboard/thingsboa…

SQLite安装配置

1.什么是 SQLite? SQLite是一个软件库,实现了自给自足的、无服务器的、零配置的、事务性的 SQL 数据库引擎。SQLite是一个增长最快的数据库引擎,这是在普及方面的增长,与它的尺寸大小无关。SQLite源代码不受版权限制。 SQLite是…

java获取文件夹下所有文件名

在进行 Java编程的过程中,我们会经常使用到文件夹下的所有文件名。有时候可能不太熟悉 Java编程的小伙伴们会发现,在代码中没有获取到所有的文件名,那么这个时候我们应该怎么去获取到这些文件呢?在进行 Java编程的过程中&#xff…

得物直播低延迟探索 | 得物技术

1.背景 直播的时效性保证了良好的用户体验,根据经验在交易环节,延迟越低转化效果也会越好。传统的直播延迟问题已经成为了一个不容忽视的问题,高延迟不仅破坏了用户的观看体验,也让主播难以实时获取到用户的反馈。为了进一步优化…

《算经》中的百钱买百鸡问题,你会做吗?试下看看(39)

小朋友们好,大朋友们好! 我是猫妹,一名爱上Python编程的小学生。 欢迎和猫妹一起,趣味学Python。 今日主题 你知道我国历史上有个王朝叫北魏吗? 北魏(386年—534年),南北朝时期北…

SimpleDateFormat和DateTimeFormatter的区别及使用详解

目录 1.简介2.区别3.SimpleDateFormat3.1 字符串转日期3.2 日期转字符串 4.DateTimeFormatter4.1 字符串转日期4.2 日期转字符串 扩展 1.简介 DateTimeFormatter 和 SimpleDateFormat 都是用于格式化日期和时间的类,但是它们有一些区别。 SimpleDateFormat 是 Jav…