如何做文件找不到显示警告



如果没有输入文件会显示错误TypeError: expected str, bytes or os.PathLike object, not NoneType,但我想显示一个警告,提醒用户在外部输入文件,该怎么办?

def get_xml_name():
args = sys.argv
if len(args) > 1 :
args = args[1]
strA = "".join(args)
return strA

inputfile = open(get_xml_name() , 'r')
TypeError: expected str, bytes or os.PathLike object, not NoneType

而不是

with open(get_xml_name() , 'r') as f:

使用

f = open(get_xml_name() , 'r')

然后是

f.close()