我创建了一个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)