2011-10-22 17 views
42

Supongamos que el esquema para un uri es "archivo". Supongamos también que el camino comienza con '.'Archivo Uri Scheme y archivos relativos

Una ruta de ejemplo es './.bashrc'. ¿Cómo se vería el Fulluri? 'file: //./.bashrc' me parece extraño.

+2

De acuerdo con Wikipedia http://en.wikipedia.org/wiki/Uniform_resource_identifier al parecer, puede omitir el esquema y tener "./.bashrc" como uri cuando se refiere relativamente. Sin embargo, esto es solo una suposición y no estoy seguro de si realmente funciona. – Tony

+2

@Tony - Gracias, eso funciona bien para hacer referencias relativas en archivos .docx - simplemente descomprima, busque las referencias "file: /// long-absolute-path/relative-path" y reemplace con "relative-path" – tucuxi

+0

Estrictamente, omitir el prefijo no siempre funciona, ya que los URI pueden tener caracteres especiales codificados con signos de porcentaje (por ejemplo, '% 20' = espacio); dependiendo de la aplicación, es probable que necesite reemplazar los caracteres escapados con su representación real. – sleblanc

Respuesta

45

En resumen, una dirección de fichero toma la forma de:

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

o se puede omitir el anfitrión (pero no la barra):

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

pero no esto:

file://file_at_current_dir [no way] 

ni esto:

file://./file_at_current_dir [no way] 

acabo confirmó que a través de urllib2.urlopen de Python()

Más detalle de 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

¿No es posible utilizar file:/absolute/path o file: relative (aunque esto no funcionará) ya que podría eliminar la autoridad para el protocolo de archivos. –

15

Es imposible usar el archivo completo: '' URI con o '..' segmentos en la ruta sin parte raíz de esa ruta. Ya sea que use 'file: //./.bashrc' o 'file: ///./.bashrc', estas rutas no tendrán sentido. Si desea utilizar un vínculo relativo, utilizarlo sin parte del protocolo/autoridad:

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

Si desea utilizar URI completa, debe decirle a una raíz con respecto al cual su ruta relativa es:

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

Según 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 describe incluso un algoritmo de eliminación de estos "" y ".." de URI.

12

En un terminal, puede escribir "file: //$PWD/.bashrc" usando "$ PWD" para referirse al directorio actual.

+0

Hago un reemplazo en mi código – wener

+0

Esto funciona bien para rutas relativas también, "file: //$PWD/../parentchilddir/somefile.txt" – nilsmagnus