python判断input为空_如何检查fileinput是否为空?Python

我创建了一个Python程序,它从命令行运行,用户输入文件进行处理。这些文件是从fileinput读入的,并且可以对它们使用optparse选项。我的问题是,如果用户不输入任何选项或文件名,程序将不做任何操作并继续运行。如果fileinput为空,我希望程序在默认情况下显示帮助选项。在

有没有办法检查一下文件输入.input(argv)为空?它defaults to stdin when empty,但我如何事先检查它是否为空?在def parse_options():

parser = optparse.OptionParser()

parser.add_option('-o', '--output', dest='output',

default='c',

help='[c/f/h] output to (c)onsole, (f)ile or (h)tml')

parser.add_option('-s', '--sort', dest='sort',

default='pa',

help='[p/c/m/d] sort by (p)ath, (c)all frequency, (m)ean duration or (d)uration,\n'

'[a/d] sort by (a)scending or (d)escending order')

options, argv = parser.parse_args()

if options.output == 'f':

output_action = LogAnalyser.output_to_file

elif options.output == 'h':

output_action = LogAnalyser.output_to_html

else:

output_action = LogAnalyser.output_to_console

#if fileinput.input(argv) is None:

# parser.print_help()

# quit()

return output_action, options.sort, fileinput.input(argv)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值