2011-10-22 8 views
42

uriのスキームが「ファイル」であると仮定します。また、パスが '。'で始まると仮定します。ファイルUriスキームと相対ファイル

パスの例は './.bashrc'です。フルリィはどのように見えるでしょうか? 'file://./.bashrc'は私にとって奇妙に見えます。要するに

+2

ウィキペディアhttp://en.wikipedia.org/wiki/Uniform_resource_identifierによると、明らかにあなたはスキームを省略して、あなたが相対的に参照しているときに "./.bashrc"をURIとして持つことができます。しかし、これはちょうど推測であり、実際にどのように動作するかはわかりません。 – Tony

+2

@トニー - ありがとう、.docxファイル内の相対参照を作成するために正常に動作 - ちょうど解凍、 "file:/// long-absolute-path/relative-path"参照を検索し、 "相対パス"で置き換えます – tucuxi

+0

厳密に接頭辞を省略することは、URIsがパーセント記号(例えば '%20' = space)でエンコードされた特殊文字を持つことができるので、必ずしも機能しません。アプリケーションによっては、エスケープされた文字を実際の表現に置き換える必要があります。 – sleblanc

答えて

45

、ファイルの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] 

私はちょうどhttp://en.wikipedia.org/wiki/File_URI_schemeからPythonのurllib2.urlopen()

もっと詳細を経由していることが確認さ:

"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

関連する問題