Python3.3 类型=文件名错误

标签 python nameerror

import sys
import argparse
parser = argparse.ArgumentParser(description='blah blah')
parser.add_argument('reference_file', type=file, help='blah blah')
args = parser.parse_args()

当我运行上面的脚本时,出现以下错误。

NameError: name 'file' is not defined

不知道怎么回事。这在 Python 3.3 中是不允许的吗?请帮忙。

最佳答案

fileopen 的别名,已在 Python 3 中删除。

您可以改用open,但是argparse 有更好的选择:[FileType() 工厂类型]:

parser.add_argument('reference_file', type=argparse.FileType('r'), help='blah blah')

关于Python3.3 类型=文件名错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19499206/

相关文章:

Python 注入(inject) - 有这样的东西吗?

python - 重命名 pandas 数据框中的多个未命名列

python - 输入(): "NameError: name ' n' is not defined"

Python名称错误: name is not defined

python 类名未定义 NameError

Python:NameError:未定义全局名称 'foobar'

python - 这是 tkinter 中的不良编程习惯吗?

python - 在C++中模拟python的 "in"

python - 为什么如果 if 语句没有 else block ,这段代码就无法工作? - Python

python - NameError:名称 'container' 未定义 Python Webscraping