web-dev-qa-db-fra.com

Fichier Uri Scheme et fichiers relatifs

Supposons que le schéma pour un URI est "fichier". Supposons également que le chemin commence par '.' 

Un exemple de chemin est './.bashrc'. A quoi ressemblerait le fulluri? 'file: //./.bashrc' me semble étrange. 

47
user592419

En bref, une URL de fichier prend la forme de:

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

ou vous pouvez omettre l'hôte (mais pas la barre oblique):

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

mais pas ceci:

file://file_at_current_dir [no way]

ni ceci:

file://./file_at_current_dir [no way]

Je viens de confirmer que via urllib2.urlopen () de Python

Plus de détails dans 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
52
RayLuo

Il est impossible d'utiliser le fichier complet: l'URI avec '.' ou '..' segments dans le chemin sans la partie racine de ce chemin. Que vous utilisiez 'fichier: //./.bashrc' ou 'fichier: ///./.bashrc', ces chemins n'auront aucun sens. Si vous souhaitez utiliser un lien relatif, utilisez-le sans partie protocole/autorité:

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

Si vous voulez utiliser un URI complet, vous devez indiquer une racine relative à laquelle votre chemin relatif est:

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

Selon 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) 

La RFC 3986 décrit même un algorithme consistant à supprimer ces "." et ".." de l'URI.

18
kinORnirvana

Dans un terminal, vous pouvez taper "fichier: //$PWD/.bashrc" en utilisant "$ PWD" pour faire référence au répertoire en cours.

14
andz

Vous ne devez pas mettre une double barre oblique après file:. La forme correcte est

'file:.bashrc'

Voir RFC 3986 , path-rootless definition

3
Maxim Reznik

Je ne connais pas votre cas d'utilisation. 

J'ai un besoin similaire dans mon code de noeud, alors quand j'ai besoin d'une URL de fichier par rapport à mon répertoire de travail, je crée une URL telle que ...

const url = "file://" + process.cwd() + "/" + ".bashrc";
3
Ken Lin

Dans un script shell unix, j'ai réussi à aller avec ceci:

file://`pwd`/relative-path

Dans votre cas particulier:

file://`pwd`/.bashrc
0
1234ru