-
-
Notifications
You must be signed in to change notification settings - Fork 30.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OSError when try convert URI to pathlib.Path #91504
Comments
Please show what the output in 3.10 is. |
Python 3.10 output: E:\Work\Venvs\p310\Scripts\python.exe "E:/uri_test/pathlib_test.py"
D:\Tests\samples\images\3.bmp exists=True
file:///D:/Tests/samples/images/3.bmp
D:Tests\samples\images\3.bmp exists=True
Process finished with exit code 0 Now I see that one '\' is missing in path after drive letter: |
A >>> p = pathlib.Path('file:///C:/Temp/test.txt')
>>> p.drive
''
>>> p.root
''
>>> os.fspath(p)
'file:\\C:\\Temp\\test.txt The path was parsed as an invalid relative path. Perhaps there's a case for adding a In 3.10+, >>> p.resolve()
WindowsPath('C:Temp/test.txt')
>>> os.path.realpath(p)
'C:Temp\\test.txt' The above result is a drive-relative path, i.e. it has no root path and thus depends on the working directory on drive "C:". |
I think this method >>> p = pathlib.Path().from_uri("file:///D:/Tests/samples/images/3.bmp")
>>> p.resolve()
'D:\Tests\samples\images\3.bmp' |
There is a problem with URI -> pathlib.Path -> URI conversion: >>>p = Path(r"file:///D:/Tests/samples/images/3.bmp").resolve()
>>>p
WindowsPath('D:Tests/samples/images/3.bmp')
>>>p.as_uri()
Traceback (most recent call last):
File "C:\pythons\Python310\lib\code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
File "C:\pythons\Python310\lib\pathlib.py", line 649, in as_uri
raise ValueError("relative path can't be expressed as a file URI")
ValueError: relative path can't be expressed as a file URI |
As @eryksun said: "A |
A solution that works today: from urllib.request import url2pathname
from pathlib import Path
def path_from_uri(uri):
return Path(url2pathname(uri.removeprefix('file:'))) I'd support the addition of a |
I suspect that the rules for parsing/emitting URLs will vary depending on whether the path uses Windows or POSIX semantics. If that's the case, I'd like to propose the following:
From a pathlib perspective this makes sense to me, as I'm hoping to move towards wrapping Thoughts? |
I'm closing this issue because the @barneygale, I think the suggested changes to |
If this is true, then the Path constructor should raise an exception when it's given a file URI, so that it doesn't give the appearance of supporting file URIs when in fact it doesn't. |
It's a good idea, but file URIs are also valid POSIX paths! You can |
Seems relatively unlikely someone would actually want that though? |
As a user, I want to be able to throw any string at |
The current implementation is, IMO, the worst option: leave
|
It works fine for me on Linux:
|
I agree with @barneygale : There's no reason to prohibit filenames starting with "file:". They're perfectly valid, at least on POSIX systems. |
Also, I don't think we'd want to implement any "strip of leading 'file:' characters" logic just on Windows. |
To clarify the case on Windows, the NTFS and ReFS filesystems support file streams of the form "filename:streamname:streamtype", "filename:streamname" ("$DATA" stream type), and "filename::streamtype" (anonymous or default stream name for the given stream type). But just "file:" without a stream name or stream type is invalid, and "file:///foo/bar/baz" is invalid because "///foo/bar/baz" is parsed as the stream name, and stream names reserve "/", ":", and NUL as invalid name characters. |
When try to convert URI to pathlib.Path I get an error:
Code to reproduce:
Python version: 3.9
With 3.10 works.
The text was updated successfully, but these errors were encountered: