|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
os.environ['HOME'] |
Hauptordner des Benutzers |
os.getpid() |
Prozess-ID |
os.system(Kommando) |
a) os.system(program) # synchron b) subprocess.call([program, "parameter"]) # synchron c)os.popen("notepad.exe") # asynchron in Python, synchron in QGis d) os.startfile(programm) # asynchron
e) os.spawnl(mode, path, ...) f) os.spawnle(mode, path, ..., env) g) os.spawnlp(mode, file, ...) ih os.spawnlpe(mode, file, ..., env) ji os.spawnv(mode, path, args) j) os.spawnve(mode, path, args, env) k) os.spawnvp(mode, file, args) l) os.spawnvpe(mode, file, args, env)¶
Beispiele:
os.spawnv(os.P_NOWAIT, "c:/windows/system32/cmd.exe",['/K', '']) os.system("mkdir v1") # Entspricht einem DOS-Befehl |
os.popen("ls *.txt") os.popen("dir *.txt") |
Entspricht dem $-Befehl |
linebreaker Zeilentrenner \r\n oder \n |
os.linesp |
Pathseparator |
os.pathsep |
|
|
|
|
|
|
|
|
|
|
|
|
os.environ['HOME'] |
Hauptordner des Benutzers |
Befehl |
Bedeutung |
access(path,mode) |
Prüft die Rechte der Datei/Ordner mit dem Modus F_OK Existiert der Pfad R_OK Leserechte W_OK Schreibrechte X_OK Ausführbare rechte |
chdir(path) |
wechselt das Verzeichnis Siehe Beispiel "Fielmanager1.py" |
getcwd() |
siehe pwd, get Current Working Directory |
getcwdb() |
Wie getcwd, Rückgabe als Byte-Array |
chmod(path, mode) |
siehe Unix-Befehl |
listdir(path) |
Liste der Dateien und Ordnern Siehe Beispiel "Fielmanager1.py" |
mkdir(path[, mode]) |
Erzeugt ein Verzeichnis |
makedirs(path[, mode]) |
Erzeugt auch komplette Pfade |
remove(path) |
Löscht eine Datei |
removedirs(path) |
Löscht alle Ordner, sofern diese leer sind |
rename(src, dest) |
Umbenennen |
renames(src, dest) |
Wie rename, legt aber eventuelle Verzeichnisse des Zielpfades an. Außerdem werden in src-Pfad leere Ordner gelöscht. |
rmdir(path) |
Löscht den leeren Ordner |
|
|
|
|
|
|
# coding=utf8
import tkinter
from tkinter import messagebox
import os
import os.path
class MyApp(tkinter.Frame):
def __init__(self, master=None):
tkinter.Frame.__init__(self, master)
self.pack(expand=True, fill="both")
self.setGUI()
print(os.getcwd())
def setGUI(self):
# JPanel fuer ui-Elemente
frame1 = tkinter.Frame(self)
frame1.pack(expand=True, fill="both", side="top", padx="5", pady="5")
frameButton = tkinter.Frame(self)
frameButton.pack(fill="x", side="bottom", padx="5", pady="5")
sb = tkinter.Scrollbar(frame1)
sb.pack(fill="y",side="right")
self.listbox = tkinter.Listbox(frame1,selectmode="single")
self.listbox.pack(fill="both",expand=True)
self.listbox["yscrollcommand"] = sb.set
sb["command"]=self.listbox.yview
self.path = os.getcwd()
self.insertDirectories()
self.bnEsc = tkinter.Button(frameButton)
self.bnEsc["text"] = "Ende"
self.bnEsc["command"] = self.quit
self.bnEsc.pack(side="right")
self.bnAction = tkinter.Button(frameButton)
self.bnAction["text"] = "Change Dir"
self.bnAction["command"] = self.cdClick
self.bnAction.pack(side="right",padx="5")
def insertDirectories(self):
items = os.listdir(self.path)
self.listbox.delete(0,"end")
self.listbox.insert("end","..")
for item in items:
if os.path.isdir(self.path+"\\"+item):
print("verz:",item)
dir(self.path+item+"\\")
self.listbox.insert("end",item)
def cdClick(self):
index = self.listbox.curselection()
dir = self.listbox.get(index)
if dir=="..":
os.chdir("..")
else:
os.chdir(dir)
self.path = os.getcwd()
messagebox.askokcancel( "Meldung", dir+"\r\n"+self.path)
self.insertDirectories()
root = tkinter.Tk()
root.title("Filemanager")
root.geometry("250x300")
app = MyApp(root)
app.mainloop()
|
|
|
|
|
|
|
|
|
|
|
|
Filemanager mit den Verzeichnissen
|
|
|
Sample1.py
os.path
|
|
|
|
|