65 lines
1.2 KiB
Python
65 lines
1.2 KiB
Python
import os.path
|
|
__author__="bjschuma"
|
|
__date__ ="$Dec 6, 2009 11:35:56 PM$"
|
|
|
|
import os
|
|
from message import write
|
|
|
|
|
|
# Check if path is a directory
|
|
def checkDir(path):
|
|
path = os.path.expanduser(path)
|
|
if checkPath(path) == True:
|
|
return os.path.isdir(path)
|
|
return False
|
|
|
|
|
|
# Check if path exists
|
|
def checkPath(path):
|
|
path = os.path.expanduser(path)
|
|
write("Checking if "+path+" exists", 2)
|
|
return os.path.exists(path)
|
|
|
|
|
|
def mkdir(path):
|
|
if checkDir(path)==False:
|
|
write("Creating directory: "+path,2)
|
|
os.mkdir(path)
|
|
|
|
|
|
def expandPath(path):
|
|
return os.path.expanduser(path)
|
|
|
|
|
|
# Open a file
|
|
def fopen(path,flags='r'):
|
|
path = os.path.expanduser(path)
|
|
# If we are reading a file, check that it exists
|
|
if ('r' in flags) == True:
|
|
exists = checkPath(path)
|
|
if exists == False:
|
|
write(path+" does not exist", 2)
|
|
return None
|
|
return open(path, flags)
|
|
|
|
|
|
# Return a listing of directory contents
|
|
def ls(path):
|
|
if checkDir(path) == False:
|
|
return False
|
|
list = os.listdir(path)
|
|
list.sort()
|
|
return list
|
|
|
|
|
|
|
|
# Remove a file
|
|
def rm(path):
|
|
if checkPath(path)==False:
|
|
write("File does not exist: "+path)
|
|
return;
|
|
os.remove(path)
|
|
|
|
|
|
def join(a,b):
|
|
return os.path.join(a,b) |