Seite 1 von 1
isDirectory() Funktion
Verfasst: Dienstag 3. März 2009, 12:57
von hallo02
Hallo Leute
Ich bin daran einen Explorer (like Midnight Commander) in Jython (ca. Python 2.1) zu realisieren und frage mich ob es eine Funktion (evt. von os) gibt, die true/false liefert je nachdem ob es sich um eine Datei oder ein Verzeichnis handelt.
Etwas in der Art os.isDirectory(Path)
Ansonsten würde ich mir da selber was basteln.
Vielen Dank.
Verfasst: Dienstag 3. März 2009, 13:02
von INFACT
Code: Alles auswählen
import os.path
def isPath(path):
if path==os.path.dirname(path):
return True
else:
return False
Ist das was du meinst?
Verfasst: Dienstag 3. März 2009, 13:07
von derdon
Code: Alles auswählen
In [1]: from os import path
In [2]: path.isdir?
Type: function
Base Class: <type 'function'>
String Form: <function isdir at 0x4806f1f0>
Namespace: Interactive
File: /usr/lib/python2.5/posixpath.py
Definition: path.isdir(path)
Docstring:
Test whether a path is a directory
@INFACT: a == b gibt bereits einen booleschen Wert zurück, das brauchst du nicht explizit noch einmal machen.
Verfasst: Dienstag 3. März 2009, 13:08
von busfahrer
Hallo
guck mal im Modul os.path
http://docs.python.org/library/os.path. ... le-os.path
os.path.isdir(path)
Return True if path is an existing directory. This follows symbolic links, so both islink() and isdir() can be true for the same path.
Gruß...busfahrer
Verfasst: Dienstag 3. März 2009, 13:29
von INFACT
@derdon ... mist:
Code: Alles auswählen
import os.path
def isPath(path):
return path==os.path.dirname(path)
Verfasst: Dienstag 3. März 2009, 13:44
von derdon
Busfahrer: Hast du es gerne doppelt?
Ok
Verfasst: Dienstag 3. März 2009, 14:00
von hallo02
Besten Dank an alle.
os.path.isdir() perfekt.
Verfasst: Dienstag 3. März 2009, 15:31
von helduel
@INFACT: Deine Funktion sagt nichts darüber aus, ob es path tatsächlich gibt. Außerdem gibt deine Funktion False zurück, wenn ich "/usr/local/" übergebe.
Gruß,
Manuel