Kamalakshi
Kamalakshi

Reputation: 7558

Python3.3 type=file 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()

When I run the above script I get the following error.

NameError: name 'file' is not defined

I don't know what's wrong. Is this not allowed in Python 3.3? Please help.

Upvotes: 4

Views: 1393

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122412

file was an alias for open, which has been removed in Python 3.

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')

Upvotes: 6

Related Questions