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