diff --git a/examples/python/create-rankfile.py b/examples/python/create-rankfile.py index d7f5eab0a73467b4fcf7a8e201e542392f8d3e24..b21e2ba595e6c0e067366617b25f7ed89ab94da3 100644 --- a/examples/python/create-rankfile.py +++ b/examples/python/create-rankfile.py @@ -1,21 +1,26 @@ def generate_rankfile(hostfile_path): rankfile_content = "" rank_id = 0 - with open(hostfile_path, "r") as hostfile: - hosts = hostfile.readlines() - num_hosts = len(hosts) - ranks_per_host = 64 - for host_id, host in enumerate(hosts): - hostname = host.strip() - for rank in range(ranks_per_host): - rank_content = f"rank {rank_id}={hostname} slot=0-63" - rankfile_content += rank_content + '\n' - rank_id = rank_id + 1 + try: + with open(hostfile_path, "r") as hostfile: + hosts = hostfile.readlines() + num_hosts = len(hosts) + ranks_per_host = 64 + for host_id, host in enumerate(hosts): + hostname = host.strip() + for rank in range(ranks_per_host): + rank_content = f"rank {rank_id}={hostname} slot=0-63" + rankfile_content += rank_content + '\n' + rank_id = rank_id + 1 - with open("rankfile", "w") as rankfile: - rankfile.writelines(rankfile_content) + with open("rankfile", "w") as rankfile: + rankfile.writelines(rankfile_content) - print("Rankfile generated successfully.") + print("Rankfile generated successfully.") + except FileNotFoundError: + print(f"Error: File {hostfile_path} not found.") + except Exception as e: + print(f"An error occurred: {str(e)}") # 请将hostfile_path变量替换为你自己的hostfile文件路径 hostfile_path = "hostfile.2"