首页 > 解决方案 > 如果没有文件通过,我如何退出程序?

问题描述

我有一个需要输入的程序:program.py < test.txt

如果没有文件输入,我将如何使程序退出?

这就是我阅读输入的方式:

def file():
    global index
    import sys


    tempo = sys.stdin.readlines()

    for element in tempo:

        if(index == 1):
            key_route.append(element.strip('\n').rstrip().lstrip().lower())
            index = 5
        else:
            routes.append(element.strip('\n').rstrip().lstrip().lower())

标签: python

解决方案


测试你是否在 temp 中有任何东西:

if not temp: 
    exit() 

推荐阅读