Peterstone
Peterstone

Reputation: 7449

Using the open function. How can I specify the path?

I'm trying to open a file I created and I don´t get it. I suspect that the open function is not using the proper path... How can I put the path?

>>> filehandler = open(fruits.obj,'w')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'fruits' is not defined

Upvotes: 0

Views: 459

Answers (2)

Sylvain Defresne
Sylvain Defresne

Reputation: 44553

The open function takes a string containing the path to your file as its first argument. In your case, you didn't use a string, but you told Python to use the obj property of the fruits object. As there is no fruits object, you get a NameError exception.

You should probably change your program to :

>>> filehandler = open("fruits.obj", "w")

Upvotes: 3

ismail
ismail

Reputation: 47632

Use double quotes:

>>> filehandler = open("fruits.obj",'w')

Upvotes: 1

Related Questions