[Tutor] "TypeError: open, argument 1: expected string, file found"
Marcel Preda
pm@dis.ro
Tue, 4 Jul 2000 09:48:27 +0200
> Mr. Gauld and everyone else on the list,
>
> I am sure someone can offer a quick solution to this one...
>
> I am working my way through the last half of Alan Gauld's "Case Study"
> section in his Learning to Program tutorial and I am having a problem with
> the document module. I've posted my slightly altered version at the address
> below.
>
> http://www.asahi-net.or.jp/~ns7s-ickw/python/document.py
>
> When I try to run it I get the following error:
>
> >>> Traceback (innermost last):
> File "C:\Program Files\Python\Pythonwin\pywin\framework\scriptutils.py",
> line 310, in RunScript
> exec codeObject in __main__.__dict__
> File "D:\Python\document.py", line 124, in ?
> D = HTMLDocument(file)
> File "D:\Python\document.py", line 12, in __init__
> self.infile = open(filename, "r")
> TypeError: open, argument 1: expected string, file found
> >>>
>
> If you see any other problems I am going to run into, I would of course
> appreciate the head's up.
>
> Thank you in advance,
Your class HTMLDocument inherits class TextDocument which inherits Document
class
The __init__ method in Document class expect a string (filename)
def __init__(self, filename):
self.filename = filename
self.infile = open(filename, "r")
On the other side when you create the `D' object you pass a `file' object
enterfile = raw_input("Enter file: ")
file = open(enterfile, "r")
D = HTMLDocument(file)
So, at the end you will call __init__(self,file) (and file is a `file type' not
a `string')
What could you do?
#1 Modify the __init_ method in Document class is not a good idea
#2 create `D' like this:
enterfile = raw_input("Enter file: ")
D = HTMLDocument(enterfile)
#`enterfile' is a string
PM