Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[python] Kleines Problem mit os.path.isdir()
View unanswered posts
View posts from last 24 hours
View posts from last 7 days

 
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) Diskussionsforum
View previous topic :: View next topic  
Author Message
Vortex375
Veteran
Veteran


Joined: 03 Mar 2005
Posts: 1739
Location: Deutschland

PostPosted: Sat Oct 28, 2006 9:02 pm    Post subject: [python] Kleines Problem mit os.path.isdir() Reply with quote

Hallo zusammen,

ich möchte überprüfen, ob es sich bei einem Pfad um ein Verzeichnis handelt. Wenn ich os.path.isdir(pfad) aufrufe erhalte ich:
Code:

TypeError: stat() argument 1 must be (encoded string without NULL bytes), not str

[soviel für die Ungeduldigen :wink: ]

Jetzt eine genauere Beschreibung:

Ich schreibe hier gerade an meinem xmms2-Client. Wenn ein Element (z.B. aus einem Dateibrowser) auf die Playlist meines Clients gedroppt wird (und es sich bei den gedroppten daten um "text/plain" oder "text/uri-list" handelt), wird ein drag-and-drop-handler aufgerufen. Dieser muss nun (unter anderem) festellen, ob es sich bei dem gedroppten Pfad um ein Verzeichnis handelt und entsprechend den xmms2-server anweisen, dieses rekursiv einzulesen.

Wenn ich mir diese Pfadangaben mit print Testweise ausgeben lasse, dann enden die zum Teil auf "\x00". Ich denke mal, dass sich os.path.isdir daran stört.

Beispiel:
(Ich ziehe das Verzeichnis 'foo' aus meinem home-Ordner auf die Playlist)
Code:

uri-list: ['file:///home/ich/foo\x00']
Traceback (most recent call last):
  File "/home/ich/python/Asterisk/playlist.py", line 126, in dropMimeData
    if os.path.isdir(entry):
  File "/usr/lib/python2.4/posixpath.py", line 195, in isdir
    st = os.stat(path)
TypeError: stat() argument 1 must be (encoded string without NULL bytes), not str


String Objekte besitzen ja eine Funktion encode(), kann ich das damit evtl. irgendwie umwandeln und diese NULL bytes loswerden?

python-Experten sind gefragt (bin selbst noch Anfänger). :)
Back to top
View user's profile Send private message
Necoro
Veteran
Veteran


Joined: 18 Dec 2005
Posts: 1912
Location: Germany

PostPosted: Sat Oct 28, 2006 9:14 pm    Post subject: Reply with quote

Also spontan kenne ich keine eingebaute version ... aber wie wäre es einfach mit:
Code:
if path[-1] == "\x00": path = path[:-1]

;) - das schneidet einfach das letzte zeichen ab ;)
_________________
Inter Deum Et Diabolum Semper Musica Est.
Back to top
View user's profile Send private message
Vortex375
Veteran
Veteran


Joined: 03 Mar 2005
Posts: 1739
Location: Deutschland

PostPosted: Sat Oct 28, 2006 9:30 pm    Post subject: Reply with quote

Quote:
das schneidet einfach das letzte zeichen ab

Danke, das funktioniert soweit schonmal.

Nur leider hab ich jetzt auch schon das nächste Problem bemerkt. isdir() erkennt aus irgend einem Grund nicht richtig ob es sich um ein Verzeichnis handelt oder nicht.

Beispiel (direkt am python-Interpreter ausgeführt):
Code:

>>> os.path.isdir("file:///home/ich/foo")
False

Bei /home/ich/foo handelt es sich aber mit Sicherheit um ein Verzeichnis.

Als Folge davon passiert jetzt, wenn man ein Verzeichnis auf die Playlist meines Clients droppt, einfach gar nichts, weil der Server versucht das Verzeichnis als einzelne Datei zu behandeln.
(Ok, eigentlich könnte man erwarten, dass der xmms2-Server selbst feststellen kann, ob es sich bei einem Pfad um ein Verzeichnis handelt oder nicht, aber das ist ein anderes Thema. Nen Bugreport gibt's aber bereits.)
Back to top
View user's profile Send private message
Necoro
Veteran
Veteran


Joined: 18 Dec 2005
Posts: 1912
Location: Germany

PostPosted: Sat Oct 28, 2006 9:51 pm    Post subject: Reply with quote

ähm ... welchen sinn hat das "file://" -- mit dem kann os.path.isdir nix anfangen ;) ... also wenn der server dir das generiert: schneid es ab - wenn nicht: lass es weg ;)
_________________
Inter Deum Et Diabolum Semper Musica Est.
Back to top
View user's profile Send private message
Vortex375
Veteran
Veteran


Joined: 03 Mar 2005
Posts: 1739
Location: Deutschland

PostPosted: Sat Oct 28, 2006 10:33 pm    Post subject: Reply with quote

Der Pfad wird so mit dem drag-and-drop übergeben, also mit dem "file://". Evtl. handelt es sich hier um das Standard-Encoding für "text/uri-list".

Der xmms2-Server will eine Pfadangabe übrigens auch mit "file://" vorn dran haben.

Mhh, wie ich es hasse an so Strings rumzuschnipfeln. Warum kann da denn auch nix einheitlich sein.

Oder gibt es da ein python-Modul, welches so Url-like Encodierte Pfadangaben in "normale" Pfade umwandeln kann?
Back to top
View user's profile Send private message
Necoro
Veteran
Veteran


Joined: 18 Dec 2005
Posts: 1912
Location: Germany

PostPosted: Sun Oct 29, 2006 9:57 am    Post subject: Reply with quote

Code:
>>> from urlparse import urlparse
>>> from os.path import isdir
>>> file = "file:///home/necoro/fotos"
>>> split = urlparse(file)
>>> print split
('file', '', '/home/necoro/fotos', '', '', '')
>>>
>>> isdir(file)
False
>>> file = split[2]
>>> print file
/home/necoro/fotos
>>> isdir(file)
True


ergo: benutze das Modul urlparse :) (dem waren eben 2 Minuten gucken in der Python-Doc vorausgegangen ... das hättest du auch hinbekommen, oder? ;))

/edit: nur der Vollständigkeit halber
Code:
>>> urlparse("/home/necoro/fotos")
('', '', '/home/necoro/fotos', '', '', '')


Sprich: lass die Pfadangaben immer mit urlparse bearbeiten - es kommt das richtige raus, auch wenn kein file:// vorne dran steht ;)
_________________
Inter Deum Et Diabolum Semper Musica Est.
Back to top
View user's profile Send private message
Vortex375
Veteran
Veteran


Joined: 03 Mar 2005
Posts: 1739
Location: Deutschland

PostPosted: Sun Oct 29, 2006 10:14 am    Post subject: Reply with quote

Quote:
das hättest du auch hinbekommen, oder?

Vermutlich. :oops:

Quote:
lass die Pfadangaben immer mit urlparse bearbeiten - es kommt das richtige raus, auch wenn kein file:// vorne dran steht


Spitze, dann sollte damit jetzt eigentlich alles funktionieren.

Vielen Dank nochmal! :D
Back to top
View user's profile Send private message
STiGMaTa_ch
Veteran
Veteran


Joined: 28 Dec 2004
Posts: 1686
Location: Rüti ZH / Schweiz

PostPosted: Sun Oct 29, 2006 11:32 am    Post subject: Reply with quote

Nur mal so als Zwischenbemerkung.

http://www.python-forum.de/

Wäre viel der bessere Ort um sowas zu diskutieren/fragen. Da hat es auch sehr versierte Cracks!

Lieber Gruss
STiGMaTa
_________________
Ich bin Schuldknappe. Das bedeutet ich bin immer an allem Schuld. Und das nicht zu knapp! | Der alltägliche Familienwahnsinn auf meinem BLOG
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) Diskussionsforum All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum