【Python】文件复制并打包成zip

1. 编写python脚本


import shutil
import os
import zipfile

# 定义源文件夹和目标文件夹路径
src_dir = '/home/cathay10/workSpace/log/folder_XjM0C1/folder_Sg5t3B/folder_ZUWK8y/folder_x6emUW/folder_vMfhib'
dst_dir = '/home/cathay10/workSpace/logs/d'

# 使用shutil.copytree()复制文件夹
try:
    # 如果目标目录不存在,copytree会尝试创建它
    shutil.copytree(src_dir, dst_dir,dirs_exist_ok=True)
    print(f"文件夹 {src_dir} 已成功复制到 {dst_dir}。")
except FileExistsError as e:
    print(f"文件夹 {dst_dir} 已存在,无法复制。")
except Exception as e:
    print(f"复制过程中发生错误:{e}")


# 定义压缩后文件的路径
compressed_file = '/home/cathay10/workSpace/logs/compressed.zip'

# 使用zipfile创建ZIP压缩文件
try:
    with zipfile.ZipFile(compressed_file, 'w', zipfile.ZIP_DEFLATED) as zipf:
        for root, dirs, files in os.walk(dst_dir):
            for file in files:
                file_path = os.path.join(root, file)
                zipf.write(file_path, os.path.relpath(file_path, dst_dir))
    print(f"文件夹 {dst_dir} 已成功压缩为 {compressed_file}。")
except Exception as e:
    print(f"压缩过程中发生错误:{e}")

# 清理:如果不需要保留原始文件夹,可以删除它
# 注意:谨慎使用,这将永久删除文件夹!
# shutil.rmtree(dst_dir)

2. 打包成可执行文件

2.1 更新软件包情况

sudo apt update

2.2 安装pip

sudo apt install python3-pip

如果需要为Python 2安装pip,则使用

sudo apt install python-pip

2.3 安装pyinstaller

pip install pyinstaller

记录安装的位置,或者指定安装位置
在这里插入图片描述

2.4 编译成可执行文件

Linux版本

/home/cathay10/.local/bin/pyinstaller --onefile /home/cathay10/workSpace/python/copyDir.py

Windows版本

pyinstaller --onefile /home/cathay10/workSpace/python/copyDir.py

macOS版本

pyinstaller --onefile -D /home/cathay10/workSpace/python/copyDir.py

注意查看编译完成的文件路径,下列为打包信息

174 INFO: PyInstaller: 6.6.0, contrib hooks: 2024.6
174 INFO: Python: 3.8.2
188 INFO: Platform: Linux-5.4.18-63-generic-x86_64-with-glibc2.29
188 INFO: wrote /home/cathay10/.local/bin/copyDir.spec
190 INFO: Extending PYTHONPATH with paths
['/home/cathay10/workSpace/python']
303 INFO: checking Analysis
303 INFO: Building Analysis because Analysis-00.toc is non existent
303 INFO: Running Analysis Analysis-00.toc
303 INFO: Target bytecode optimization level: 0
303 INFO: Initializing module dependency graph...
304 INFO: Caching module graph hooks...
313 INFO: Analyzing base_library.zip ...
928 INFO: Loading module hook 'hook-heapq.py' from '/home/cathay10/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
1166 INFO: Loading module hook 'hook-encodings.py' from '/home/cathay10/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
2191 INFO: Loading module hook 'hook-pickle.py' from '/home/cathay10/.local/lib/python3.8/site-packages/PyInstaller/hooks'...
2938 INFO: Caching module dependency graph...
3050 INFO: Looking for Python shared library...
3088 INFO: Using Python shared library: /lib/x86_64-linux-gnu/libpython3.8.so.1.0
3088 INFO: Analyzing /home/cathay10/workSpace/python/copyDir.py
3090 INFO: Processing module hooks...
3099 INFO: Performing binary vs. data reclassification (2 entries)
3103 INFO: Looking for ctypes DLLs
3106 INFO: Analyzing run-time hooks ...
3112 INFO: Looking for dynamic libraries
3237 INFO: Warnings written to /home/cathay10/.local/bin/build/copyDir/warn-copyDir.txt
3250 INFO: Graph cross-reference written to /home/cathay10/.local/bin/build/copyDir/xref-copyDir.html
3259 INFO: checking PYZ
3259 INFO: Building PYZ because PYZ-00.toc is non existent
3259 INFO: Building PYZ (ZlibArchive) /home/cathay10/.local/bin/build/copyDir/PYZ-00.pyz
3422 INFO: Building PYZ (ZlibArchive) /home/cathay10/.local/bin/build/copyDir/PYZ-00.pyz completed successfully.
3435 INFO: checking PKG
3435 INFO: Building PKG because PKG-00.toc is non existent
3435 INFO: Building PKG (CArchive) copyDir.pkg
5484 INFO: Building PKG (CArchive) copyDir.pkg completed successfully.
5485 INFO: Bootloader /home/cathay10/.local/lib/python3.8/site-packages/PyInstaller/bootloader/Linux-64bit-intel/run
5485 INFO: checking EXE
5485 INFO: Building EXE because EXE-00.toc is non existent
5485 INFO: Building EXE from EXE-00.toc
5485 INFO: Copying bootloader EXE to /home/cathay10/.local/bin/dist/copyDir
5487 INFO: Appending PKG archive to custom ELF section in EXE
5499 INFO: Building EXE from EXE-00.toc completed successfully.

2.5 进入对应文件夹,运行可执行文件即可

/home/cathay10/.local/bin/dist/copyDir
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将 Python 程序打包 exe 文件,可以使用 PyInstaller 工具。以下是使用 PyInstaller 将 Python 程序打包 exe 文件的简单步骤: 1. 安装 PyInstaller 在命令行中使用以下命令进行安装: ``` pip install pyinstaller ``` 2. 创建 spec 文件 在命令行中导航到你的 Python 程序所在的目录,并使用以下命令创建一个 spec 文件: ``` pyinstaller --name=程序名称 --onefile --windowed 程序文件名.py ``` 其中,`--name` 参数指定生的 exe 文件的名称,`--onefile` 参数指定生的 exe 文件是单个文件而不是文件夹,`--windowed` 参数指定生的 exe 文件没有控制台窗口。 执行该命令后,PyInstaller 会在当前目录下创建一个 spec 文件,以及一个名为 `build` 的文件夹和一个名为 `dist` 的文件夹。 3. 编辑 spec 文件 打开生的 spec 文件,可以看到其中包含了一些关于程序的设置和依赖库的信息。你可以编辑这个文件来自定义程序的设置。 例如,你可以添加以下代码来设置程序的图标: ```python exe = EXE(pyz, a.scripts, a.binaries, a.zipfiles, a.datas, [], name='程序名称', icon='程序图标.ico', ... ) ``` 4. 打包程序 在命令行中使用以下命令来打包程序: ``` pyinstaller 程序文件名.spec ``` 执行该命令后,PyInstaller 会在 `dist` 文件夹中创建一个 exe 文件,这个文件就是你打包好的程序。 如果你想要将程序打包一个文件夹而不是一个 exe 文件,可以将 `--onefile` 参数改为 `--onedir`。 另外,如果你的程序依赖于一些第三方库,PyInstaller 可能无法自动识别这些库,这时你需要使用 `--hidden-import` 参数来手动添加这些库。例如,如果你的程序依赖于 Pillow 库,可以使用以下命令来打包程序: ``` pyinstaller --name=程序名称 --onefile --windowed --hidden-import=Pillow 程序文件名.py ``` 打包后,你就可以在 `dist` 文件夹中找到生的 exe 文件,将它复制到其他地方运行即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值