2011-10-22 62 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