python file to .exe
Install
pip install pyinstaller
python 文件
这里举个例子:
建一个.py 文件,保存为extract_model.py
这是一个用于unzip AF zip并且extract *_model_0.cif的一个程序。需要提前把所有的zip放到一个source文件夹里,然后再新建一个dest文件夹。
import shutil
from pathlib import Path
import zipfile
import sys
import tempfile
def extract_all_zips(source_dir: Path, temp_dir: Path):
for zip_path in source_dir.rglob("*.zip"):
try:
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
unzip_target = temp_dir / zip_path.stem
unzip_target.mkdir(parents=True, exist_ok=True)
zip_ref.extractall(unzip_target)
print(f"Unzipped: {zip_path} → {unzip_target}")
except zipfile.BadZipFile:
print(f"Warning: Cannot unzip {zip_path}")
def copy_all_model_0_cif(temp_dir: Path, dest_dir: Path):
dest_dir.mkdir(parents=True, exist_ok=True)
for cif_file in temp_dir.rglob("*_model_0.cif"):
dest_file = dest_dir / cif_file.name
shutil.copy(cif_file, dest_file)
print(f"Copied: {cif_file} → {dest_file}")
if __name__ == "__main__":
if len(sys.argv) == 3:
source_dir = Path(sys.argv[1])
dest_dir = Path(sys.argv[2])
else:
# Ask for paths if none provided
print("No source/destination provided. Please enter them manually.")
source_input = input("Enter full path to source directory: ").strip('"')
dest_input = input("Enter full path to destination directory: ").strip('"')
source_dir = Path(source_input)
dest_dir = Path(dest_input)
if not source_dir.exists():
print(f"Error: Source directory '{source_dir}' does not exist.")
input("Press Enter to exit...")
sys.exit(1)
with tempfile.TemporaryDirectory() as tmpdirname:
temp_dir = Path(tmpdirname)
extract_all_zips(source_dir, temp_dir)
copy_all_model_0_cif(temp_dir, dest_dir)
print("Done.")
input("Press Enter to exit...")
py to exe
把该.py转换成exe,注意要在windows的terminal进行,如果是ubuntu就不会生成.exe
pyinstaller --onefile extract_model.py
然后在当前dir里打开dist文件夹,里面就是.exe,运行,然后把文件夹拖进去就有path显示了。