2011-10-22 87 views
42

假設uri的方案是「文件」。還假定路徑以'。'開頭。文件Uri方案和相關文件

示例路徑是'./.bashrc'。 fulluri會如何看待? 'file://./.bashrc'對我來說顯得很奇怪。

+2

根據維基百科http://en.wikipedia.org/wiki/Uniform_resource_identifier顯然你可以忽略這個方案,並且當你指的是相對的時候就有「./.bashrc」作爲uri。但是,這只是一個猜測,我不確定它究竟是如何工作的。 – Tony

+2

@Tony - 謝謝,在.docx文件中做相對引用時工作正常 - 只需解壓,找到「file:/// long-absolute-path/relative-path」引用,並替換爲「relative-path」 – tucuxi

+0

嚴格省略前綴並不總是有效,因爲URI可以使用百分號(例如'%20' = space)編碼的特殊字符;取決於應用程序,您可能需要用其實際表示來替換轉義字符。 – sleblanc

回答

45

總之,一個文件URL的格式如下:

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

,或者你可以省略host(但不是斜線):

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

但不是這樣的:

file://file_at_current_dir [no way] 

也沒有:

file://./file_at_current_dir [no way] 

我只是確認,通過Python的urllib2.urlopen()

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 
+0

是否也可以使用file:/ absolute/path或file:relative(即使這不起作用),因爲您可以刪除文件協議的權限。 –

15

這是不可能使用完整的文件: '' 與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的「..」。

12

在終端中,您可以使用「$ PWD」鍵入「file://$PWD/.bashrc」來引用當前目錄。

+0

我在我的代碼中進行替換 – wener

+0

這對於相對路徑以及「file://$PWD/../parentchilddir/somefile.txt」 – nilsmagnus