getting an inputStream on a file

jano jnana4 at DELETEhotmailCAPS.com
Mon Aug 19 01:18:01 EDT 2002


"Peter Hansen" <peter at engcorp.com> wrote in message
news:3D605276.94935AD4 at engcorp.com...
> jano wrote:
> >
> > Is there a way to get a stream on a file, for methods that require a
stream
> > as input when I want to use a file as the input.
>
> What does "stream" mean to you in this context?  Which methods require
> a "stream"?
>
> -Peter

I was trying to do the following:

>>> from xml.dom.ext.reader.Sax2 import FromXmlStream
>>> doc = FromXmlStream("foosubset.xml")
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in ?
    doc = FromXmlStream("foosubset.xml")
  File "C:\PYTHON22\Lib\site-packages\_xmlplus\dom\ext\reader\Sax2.py", line
387, in FromXmlStream
    return reader.fromStream(stream, ownerDocument)
  File "C:\PYTHON22\Lib\site-packages\_xmlplus\dom\ext\reader\Sax2.py", line
372, in fromStream
    s = saxutils.prepare_input_source(stream)
  File "C:\PYTHON22\Lib\site-packages\_xmlplus\sax\saxutils.py", line 463,
in prepare_input_source
    f = urllib.urlopen(source.getSystemId())
  File "C:\Python22\Lib\urllib.py", line 73, in urlopen
    return _urlopener.open(url)
  File "C:\Python22\Lib\urllib.py", line 178, in open
    return getattr(self, name)(url)
  File "C:\Python22\Lib\urllib.py", line 405, in open_file
    return self.open_local_file(url)
  File "C:\Python22\Lib\urllib.py", line 412, in open_local_file
    stats = os.stat(localname)
OSError: [Errno 2] No such file or directory: 'foosubset.xml'
>>>

It works fine if the method is FromXmlStream(sys.stdin), but I wanted to use
a file, and I wondered if it was possible to get a stream (like an
InputStream in Java) on a file in order to satisfy the method.  Perhaps this
is a Java idiom that doesn't translate well into Python.  The signature of
the method I wanted to use was
FromXmlStream(stream, ownerDocument=None, validate=0, keepAllWs=0,
catName=None, saxHandlerClass=<class xml.dom.ext.reader.Sax2.XmlDomGenerator
at 0x00995690>, parser=None), so i thought there might be a way other than s
tdin redirection to get use that method on a file.

jano.







More information about the Python-list mailing list