[PythonCE] Current Directory emulation (fix)
Telion
telionce@yahoo.com
Wed, 2 Oct 2002 11:02:34 -0700 (PDT)
I pasted wrong one in the earlier post.
Please try this one instead.
### os.getcwd, os.chdir emulation + os.listdir fix ###
import os
os.cwd = "\\"
def __getcwd(): return os.cwd
os.getcwd = __getcwd
def __fullpath(p):
if not p or type(p) != str:
raise OSError, "Invalid path"
if not p[0] in "/\\":
p = os.path.join(os.cwd,p)
p = p.replace('/','\\')
pl = p.split('\\')
#print pl
i =0
while 1:
if i >= len(pl):
break
#print i, pl[i]
if pl[i] =='..':
try:
pl.pop(i)
pl.pop(i-1)
except:
raise OSError, "Invalid path"
i -= 1
if i < 0:
raise OSError, "Invalid path"
elif not pl[i]:
pl.pop(i)
else:
i += 1
return "\\"+"\\".join(pl)
def __chdir(p):
fullpath = __fullpath(p)
if os.path.isdir(fullpath):
os.cwd = fullpath
else:
raise OSError, "Invalid path"
os.chdir = __chdir
# os.listdir may fails when "\\storage card\\../" is the given path
# It does not fail when "\\wndows\\..\\" or "/Program files/../"
# Probably something related with CF or memory card.
# So, give the aboslute path, instead.
os.oldlistdir = os.listdir
def __listdirfix(p):
if p != '':
p = __fullpath(p)
#print p
if p == "\\":
p = ''
return os.oldlistdir(p)
os.listdir = __listdirfix
#### END OF CE FIX ###
=====
Telion
- telionce@yahoo.com -
http://pages.ccapcable.com
__________________________________________________
Do you Yahoo!?
New DSL Internet Access from SBC & Yahoo!
http://sbc.yahoo.com