OVH Cloud OVH Cloud

petit pb de path

7 réponses
Avatar
Olivier Ravard
Bonjour,

D'un seul coup, j'ai un trou... pour un pb tout bête de path sous windoze.
Je récupère le nom d'un fichier qui est par exemple
a = 'c:\monrep\home\rep\toto.txt'

si j'ouvre le fichier :
f=open(a) --> [Errno 2] No such file or directory:
'c:\\monrep\\home\rep\toto.txt'

bon. alors je veux séparer :
os.path.split(a) --> ('c:\\cmonrep', 'home\rep\toto.txt')

C'est normal car \r et \t sont interprêtés spécifiquement... et
a.split('\\') --> ['c:', 'cmonrep', 'home\rep\toto.txt']

Comment faire ?? Il y a une solution toute simple j'en suis sûr...

Merci.

O.R.

7 réponses

Avatar
Jerome
Olivier Ravard wrote:
Bonjour,

D'un seul coup, j'ai un trou... pour un pb tout bête de path sous windoze.
Je récupère le nom d'un fichier qui est par exemple
a = 'c:monrephomereptoto.txt'

si j'ouvre le fichier :
f=open(a) --> [Errno 2] No such file or directory:
'c:monrephomereptoto.txt'

bon. alors je veux séparer :
os.path.split(a) --> ('c:cmonrep', 'homereptoto.txt')

C'est normal car r et t sont interprêtés spécifiquement... et
a.split('') --> ['c:', 'cmonrep', 'homereptoto.txt']

Comment faire ?? Il y a une solution toute simple j'en suis sûr...

Merci.

O.R.



Et écrire simplement a='c:monrephomereptoto.txt' ? ça marche pas ?


[troll on]
sous linux ça marche mieux ;-)
[troll off]

Avatar
Olivier Ravard
"Jerome" a écrit dans le message de news:
cs648v$q94$
Olivier Ravard wrote:
Bonjour,

D'un seul coup, j'ai un trou... pour un pb tout bête de path sous
windoze.


Je récupère le nom d'un fichier qui est par exemple
a = 'c:monrephomereptoto.txt'

si j'ouvre le fichier :
f=open(a) --> [Errno 2] No such file or directory:
'c:monrephomereptoto.txt'

bon. alors je veux séparer :
os.path.split(a) --> ('c:cmonrep', 'homereptoto.txt')

C'est normal car r et t sont interprêtés spécifiquement... et
a.split('') --> ['c:', 'cmonrep', 'homereptoto.txt']

Comment faire ?? Il y a une solution toute simple j'en suis sûr...

Merci.

O.R.



Et écrire simplement a='c:monrephomereptoto.txt' ? ça marche pas ?




Non car la chaîne vient d'un file dialog. La chaîne n'est pas écrite à la
main.


[troll on]
sous linux ça marche mieux ;-)


Eh oui, mais je développe l'apli sous les deux systèmes...

[troll off]





Avatar
F. Petitjean
Bonjour,

D'un seul coup, j'ai un trou... pour un pb tout bête de path sous windoze.
Je récupère le nom d'un fichier qui est par exemple
a = 'c:monrephomereptoto.txt'

si j'ouvre le fichier :
f=open(a) --> [Errno 2] No such file or directory:
'c:monrephomereptoto.txt'

bon. alors je veux séparer :
os.path.split(a) --> ('c:cmonrep', 'homereptoto.txt')

C'est normal car r et t sont interprêtés spécifiquement... et
a.split('') --> ['c:', 'cmonrep', 'homereptoto.txt']

Comment faire ?? Il y a une solution toute simple j'en suis sûr...
Votre question est loin d'être claire


import os, os.path

fname = r'C:monrephomereptoto.txt'
# ou même fname = 'c:/monrep/home/rep/toto.txt'

La fonction os.path.exists(fname) me semble la première chose à essayer.
La fonction os.path.split(fname) donne un tuple (dirname, basename)
Le séparateur '/' ou '' est dans os.path.sep
et donc pour tout séparer :
elements = fname.split(os.path.sep)
mais attention, on peut obtenir un premier élément vide par exemple sur
un système posix avec fname = '/monrep/home/rep/toto.txt'
fname.split(os.path.sep) donne ['', 'monrep', 'home', 'rep', 'toto.txt']

Le module "path" de Jason Orendorff pourrait vous être utile :
http://www.jorendorff.com/articles/python/path

Merci.

O.R.





Avatar
Amaury Forgeot d'Arc
Bonjour,

D'un seul coup, j'ai un trou... pour un pb tout bête de path sous windoze.
Je récupère le nom d'un fichier qui est par exemple
a = 'c:monrephomereptoto.txt'

si j'ouvre le fichier :
f=open(a) --> [Errno 2] No such file or directory:
'c:monrephomereptoto.txt'

bon. alors je veux séparer :
os.path.split(a) --> ('c:cmonrep', 'homereptoto.txt')

C'est normal car r et t sont interprêtés spécifiquement... et
a.split('') --> ['c:', 'cmonrep', 'homereptoto.txt']

Comment faire ?? Il y a une solution toute simple j'en suis sûr...

Merci.

O.R.



Comme tu l'a dit, c'est parce que r et t sont interprétés spécialement

dans une constante chaïne.
Trois solutions:
- doubler tous les
a = 'c:monrephomereptoto.txt'
- utiliser / comme séparateur
a = 'c:/monrep/home/rep/toto.txt'
- mettre le préfixe r devant la chaîne
a = r'c:monrephomereptoto.txt'

Amaury

Avatar
Olivier Ravard
"Amaury Forgeot d'Arc" a écrit dans le message de news:
cs6enh$ocq$
Bonjour,

D'un seul coup, j'ai un trou... pour un pb tout bête de path sous
windoze.


Je récupère le nom d'un fichier qui est par exemple
a = 'c:monrephomereptoto.txt'

si j'ouvre le fichier :
f=open(a) --> [Errno 2] No such file or directory:
'c:monrephomereptoto.txt'

bon. alors je veux séparer :
os.path.split(a) --> ('c:cmonrep', 'homereptoto.txt')

C'est normal car r et t sont interprêtés spécifiquement... et
a.split('') --> ['c:', 'cmonrep', 'homereptoto.txt']

Comment faire ?? Il y a une solution toute simple j'en suis sûr...

Merci.

O.R.



Comme tu l'a dit, c'est parce que r et t sont interprétés spécialement

dans une constante chaïne.
Trois solutions:
- doubler tous les
a = 'c:monrephomereptoto.txt'


Je ne peux pas. Le split ne me permet pas de repérer les

- utiliser / comme séparateur
a = 'c:/monrep/home/rep/toto.txt'


Je ne peux pas non plus. La chaîne est donnée par un FileDialog.

- mettre le préfixe r devant la chaîne
a = r'c:monrephomereptoto.txt'


Ah ça oui. Et ça marche... Je ne connaissais pas cette solution. Je
pensais avoir un trou, mais en fait c'est une lacune.
Qu'elle est la signification de cette commande ?
Merci pour la solution.



Amaury



Avatar
bruno modulix
Olivier Ravard wrote:
"Amaury Forgeot d'Arc" a écrit dans le message de news:
cs6enh$ocq$



(snip)

- mettre le préfixe r devant la chaîne
a = r'c:monrephomereptoto.txt'



Ah ça oui. Et ça marche... Je ne connaissais pas cette solution. Je
pensais avoir un trou, mais en fait c'est une lacune.
Qu'elle est la signification de cette commande ?


r => 'raw string'. Ca "désactive" les séquences d'échappement.
cf :
http://www.python.org/doc/2.4/ref/strings.html


--
bruno desthuilliers
ruby -e "print ''.split('@').collect{|p|
p.split('.').collect{|w| w.reverse}.join('.')}.join('@')"
--


Avatar
Olivier Ravard
"bruno modulix" a écrit dans le message de news:
41e797e0$0$6714$
Olivier Ravard wrote:
"Amaury Forgeot d'Arc" a écrit dans le message de
news:


cs6enh$ocq$



(snip)

- mettre le préfixe r devant la chaîne
a = r'c:monrephomereptoto.txt'



Ah ça oui. Et ça marche... Je ne connaissais pas cette solution. Je
pensais avoir un trou, mais en fait c'est une lacune.
Qu'elle est la signification de cette commande ?


r => 'raw string'. Ca "désactive" les séquences d'échappement.
cf :
http://www.python.org/doc/2.4/ref/strings.html



Merci. On en apprend tous les jours (ou presque) avec python.
Du moins, c'est mon cas.


--
bruno desthuilliers
ruby -e "print ''.split('@').collect{|p|
p.split('.').collect{|w| w.reverse}.join('.')}.join('@')"
--