Created
October 15, 2025 01:38
-
-
Save meisa233/681c529bea2044e4c7749a29e35bad6f to your computer and use it in GitHub Desktop.
将多个Python文件的内容合并到一个txt文件中
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # -*- coding: utf-8 -*- | |
| import os | |
| import sys | |
| import argparse | |
| def merge_py_files(file_list, output_file="merged_output.txt"): | |
| """ | |
| 将多个Python文件的内容合并到一个txt文件中 | |
| 参数: | |
| file_list: Python文件名列表 | |
| output_file: 输出文件名,默认为 merged_output.txt | |
| """ | |
| # 检查输出文件是否已存在 | |
| if os.path.exists(output_file): | |
| overwrite = input(f"文件 {output_file} 已存在,是否覆盖?(y/n): ") | |
| if overwrite.lower() != 'y': | |
| print("操作已取消") | |
| return | |
| with open(output_file, 'w', encoding='utf-8') as outfile: | |
| for py_file in file_list: | |
| # 检查文件是否存在 | |
| if not os.path.exists(py_file): | |
| print(f"警告: 文件 {py_file} 不存在,跳过") | |
| continue | |
| # 检查是否为.py文件 | |
| if not py_file.endswith('.py'): | |
| print(f"警告: {py_file} 不是Python文件,跳过") | |
| continue | |
| try: | |
| # 读取Python文件内容 | |
| with open(py_file, 'r', encoding='utf-8') as infile: | |
| content = infile.read() | |
| # 按指定格式写入输出文件 | |
| outfile.write(f"```{py_file}\n") | |
| outfile.write(content) | |
| # 确保内容末尾有换行 | |
| if not content.endswith('\n'): | |
| outfile.write('\n') | |
| outfile.write("```\n") | |
| print(f"✓ 已添加: {py_file}") | |
| except Exception as e: | |
| print(f"错误: 读取 {py_file} 时出错 - {e}") | |
| print(f"\n合并完成!输出文件: {output_file}") | |
| def main(): | |
| """主函数 - 使用argparse处理命令行参数""" | |
| parser = argparse.ArgumentParser( | |
| description='将多个Python文件合并到一个txt文件中', | |
| formatter_class=argparse.RawDescriptionHelpFormatter, | |
| epilog=''' | |
| 使用示例: | |
| %(prog)s a.py b.py c.py # 使用默认输出文件名 | |
| %(prog)s a.py b.py -o output.txt # 指定输出文件名 | |
| %(prog)s *.py -o all_scripts.txt # 合并所有py文件 | |
| ''' | |
| ) | |
| parser.add_argument( | |
| 'files', | |
| nargs='*', | |
| help='要合并的Python文件列表' | |
| ) | |
| parser.add_argument( | |
| '-o', '--output', | |
| default='merged_output.txt', | |
| help='输出文件名 (默认: merged_output.txt)' | |
| ) | |
| args = parser.parse_args() | |
| # 如果没有提供文件,使用交互式模式 | |
| if not args.files: | |
| print("=== Python文件合并工具 ===\n") | |
| print("请输入要合并的Python文件名,每行一个") | |
| print("输入空行结束输入\n") | |
| file_list = [] | |
| while True: | |
| filename = input("文件名: ").strip() | |
| if not filename: | |
| break | |
| file_list.append(filename) | |
| if not file_list: | |
| print("未输入任何文件名") | |
| parser.print_help() | |
| return | |
| output_file = input(f"\n输出文件名 (直接回车使用默认名 {args.output}): ").strip() | |
| if not output_file: | |
| output_file = args.output | |
| merge_py_files(file_list, output_file) | |
| else: | |
| merge_py_files(args.files, args.output) | |
| if __name__ == "__main__": | |
| main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment