Схема файла Uri и относительные файлы - PullRequest
54 голосов
/ 22 октября 2011

Предположим, что схема для URI - "файл".Также предположим, что путь начинается с '.'

Пример пути - «./.bashrc».Как бы выглядел фуллури?'file: //./.bashrc' кажется мне странным.

Ответы [ 6 ]

60 голосов
/ 04 марта 2013

Короче говоря, URL файла имеет вид:

file://localhost/absolute/path/to/file [ok]

или вы можете опустить хост (но не косую черту):

file:///absolute/path/to/file [ok]

но не это:

file://file_at_current_dir [no way]

ни это:

file://./file_at_current_dir [no way]

Я только что подтвердил это с помощью urllib2.urlopen () Python ()

Подробнее от http://en.wikipedia.org/wiki/File_URI_scheme:

"file:///foo.txt" is okay, while "file://foo.txt" is not,
although some interpreters manage to handle the latter
22 голосов
/ 06 октября 2013

Невозможно использовать полный файл: URI с '.' или «..» сегменты в пути без корневой части этого пути. Используете ли вы «file: //./.bashrc» или «file: ///./.bashrc», эти пути не будут иметь смысла. Если вы хотите использовать относительную ссылку, используйте ее без протокольной / авторитетной части:

<a href="./.bashrc">link</a>

Если вы хотите использовать полный URI, вы должны указать корень, относительно которого ваш относительный путь:

<a href="file:///home/kindrik/./.bashrc">link</a>

Согласно RFC 3986

The path segments "." and "..", also known as dot-segments, are
defined for relative reference within the path name hierarchy.  They
are intended for use at the beginning of a relative-path reference
(Section 4.2) to indicate relative position within the hierarchical
tree of names.  This is similar to their role within some operating
systems' file directory structures to indicate the current directory
and parent directory, respectively.  However, unlike in a file
system, these dot-segments are only interpreted within the URI path
hierarchy and are removed as part of the resolution process (Section
5.2).

The complete path segments "." and ".." are intended only for use
within relative references (Section 4.1) and are removed as part of
the reference resolution process (Section 5.2).  However, some
deployed implementations incorrectly assume that reference resolution
is not necessary when the reference is already a URI and thus fail to
remove dot-segments when they occur in non-relative paths.  URI
normalizers should remove dot-segments by applying the
remove_dot_segments algorithm to the path, as described in Section 5.2.4.

The complete path segments "." and ".." are intended only for use
within relative references (Section 4.1) and are removed as part of
the reference resolution process (Section 5.2) 

RFC 3986 описывает даже алгоритм удаления этих "." и ".." из URI.

14 голосов
/ 14 декабря 2014

В терминале вы можете набрать «file: //$PWD/.bashrc», используя «$ PWD» для ссылки на текущий каталог.

10 голосов
/ 31 октября 2018

Вы не должны ставить двойную косую черту после file:.Правильная форма:

'file:.bashrc'

См. RFC 3986 , path-rootless определение

3 голосов
/ 18 апреля 2018

Я не знаю ваш вариант использования.

У меня аналогичная потребность в коде моего узла, поэтому, когда мне нужен URL-адрес файла относительно моего рабочего каталога, я создаю URL-адрес примерно так ...

const url = "file://" + process.cwd() + "/" + ".bashrc";
1 голос
/ 03 ноября 2018

В сценарии оболочки Unix мне удалось сделать следующее:

file://`pwd`/relative-path

В вашем конкретном случае:

file://`pwd`/.bashrc
...