python - 如果没有文件通过,我如何退出程序?
问题描述
我有一个需要输入的程序: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())
解决方案
测试你是否在 temp 中有任何东西:
if not temp:
exit()
推荐阅读
- sql - 根据 Oracle SQL 中的先前行值计算行值
- c++ - 具有跨翻译单元的潜在类重新定义的全局静态内联变量 - 这种特殊情况是 C++17 中未定义的行为吗?
- maven - maven 发布:在 azure 构建管道中准备
- yii2 - yii2 Image::thumbnail 错误:无法打开图片
- azure - Azure API Management Set-Body with Liquid 模板
- matlab - 获取对象名称作为字符串 MATLAB
- google-api - 从 Google/GMail 中的电子邮件中检索 accountId
- php - 当 Ajax 调用 GET 方法到 PHP 时重定向
- angular - 如何在 Angular 的 ngAfterViewChecked 生命周期钩子中运行一次性方法
- flutter - 使用 Getter 时返回不可修改的列表