[Pythonmac-SIG] Mac and unix pathnames
Jack Jansen
jack@oratrix.com
Mon, 21 Jan 2002 15:31:28 +0100
On Monday, January 21, 2002, at 01:58 , Michael J. Barber wrote:
> I don't disagree, but I do have an example that (I think) isn't just
> sloppy programming. It is a recent discovery, so there may be a simple
> solution that I haven't though of.
Ah, talking to external programs is exactly the sort of use case I had
in mind. Here is a function that does the trick (but I'm not 100% sure
it will work in 2.2 as distributed). Are people also interested in the
reverse, i.e. obtaining Unix pathnames in a MacPython program running on
MacOSX?
# Classicpath - convert unix pathnames (or fsspecs, or fsrefs) to
# classic MacOS colon-separated pathnames.
# Will only work in MachoPython 2.2+ and later (because part of the magic
# is that the first FSSpec() call interprets unix pathnames).
import macfs
def classicpath(item):
import macfs
fss = macfs.FSSpec(item)
vrefnum, dirid, filename = fss.as_tuple()
path = filename
while dirid != 1:
fss = macfs.FSSpec((vrefnum, dirid, ''))
vrefnum, dirid, filename = fss.as_tuple()
path = filename + ':' + path
return path
--
- Jack Jansen <Jack.Jansen@oratrix.com>
http://www.cwi.nl/~jack -
- If I can't dance I don't want to be part of your revolution -- Emma
Goldman -