import tkinter as tk
from tkinter import filedialog
import os
import subprocess
def choose_file():
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename(title="选择文件")
if file_path:
pack_files(file_path)
else:
print("未选择文件。")
def pack_files(file_path):
temp_dir = os.path.join(os.path.dirname(file_path), 'temp_package')
os.makedirs(temp_dir, exist_ok=True)
file_name = os.path.basename(file_path)
temp_file_path = os.path.join(temp_dir, file_name)
if os.name == 'nt':
os.system(f'copy "{file_path}" "{temp_file_path}"')
else:
os.system(f'cp "{file_path}" "{temp_file_path}"')
try:
subprocess.run(['pyinstaller', '--onefile', temp_file_path], check=True)
exe_file_path = os.path.join('dist', file_name)
if os.path.exists(exe_file_path):
print(f"生成的 .exe 文件位于:{exe_file_path}")
else:
print("打包失败。")
except subprocess.CalledProcessError as e:
print(f"打包过程中发生错误: {e}")
if __name__ == "__main__":
choose_file()
终端的结果:
打包过程中发生错误: Command '['pyinstaller', '--onefile', 'Y:/Pygame/大蛇无双\temp_package\snake_V1.0.00.py']' returned non-zero exit status 1.