Python3.3 type =文件名错误 [英] Python3.3 type=file NameError
本文介绍了Python3.3 type =文件名错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import sys
import argparse
parser = argparse.ArgumentParser(description='blah blah')
parser.add_argument('reference_file', type=file, help='blah blah')
args = parser.parse_args()
运行上述脚本时,出现以下错误.
When I run the above script I get the following error.
NameError: name 'file' is not defined
我不知道怎么了. Python 3.3不允许这样做吗?请帮忙.
I don't know what's wrong. Is this not allowed in Python 3.3? Please help.
推荐答案
file
是open
的别名,已在Python 3中删除.
file
was an alias for open
, which has been removed in Python 3.
您可以改用open
,但是argparse
有一个更好的选择:[FileType()
工厂类型]:
You can use open
instead, but argparse
has a better option: the [FileType()
factory type]:
parser.add_argument('reference_file', type=argparse.FileType('r'), help='blah blah')
这篇关于Python3.3 type =文件名错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文