Mike Auty <mike.a...@gmail.com> added the comment:

Here's the revised code sample:

    import pathlib
    import urllib.request
    
    path = "Z:\\test.py"
    
    print(f"Stock open: {pathlib.Path(path).as_uri()}")
    with urllib.request.urlopen(pathlib.Path(path).as_uri()) as fp:
        data = fp.read()
    
    print(f"Pathlib resolved open: {pathlib.Path(path).resolve().as_uri()}")
    with urllib.request.urlopen(pathlib.Path(path).resolve().as_uri()) as fp:
        data = fp.read()

and here's the output:

    Z:\> python test.py
    Stock open: file:///Z:/test.py
    Pathlib resolved open: file://host/share/test.py
    Traceback (most recent call last):
    File "C:\Program Files\Python310\lib\urllib\request.py", line 1505, in 
open_local_file
        stats = os.stat(localfile)
    FileNotFoundError: [WinError 2] The system cannot find the file specified: 
'\\share\\test.py'

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    File "Z:\test.py", line 14, in <module>
        with urllib.request.urlopen(pathlib.Path(path).resolve().as_uri()) as 
fp:
    File "C:\Program Files\Python310\lib\urllib\request.py", line 216, in 
urlopen
        return opener.open(url, data, timeout)
    File "C:\Program Files\Python310\lib\urllib\request.py", line 519, in open
        response = self._open(req, data)
    File "C:\Program Files\Python310\lib\urllib\request.py", line 536, in _open
        result = self._call_chain(self.handle_open, protocol, protocol +
    File "C:\Program Files\Python310\lib\urllib\request.py", line 496, in 
_call_chain
        result = func(*args)
    File "C:\Program Files\Python310\lib\urllib\request.py", line 1483, in 
file_open
        return self.open_local_file(req)
    File "C:\Program Files\Python310\lib\urllib\request.py", line 1522, in 
open_local_file
        raise URLError(exp)
    urllib.error.URLError: <urlopen error [WinError 2] The system cannot find 
the file specified: '\\share\\test.py'>

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue46654>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to