Pretty close! One translation into Python would be:
import string
fp = open( filename, "r" )
while 'True' :
fields = string.splitfields( fp.readline(), '\t' )
if not fields : break
process( fields[0], fields[1:] )
If you want to split on multiple-whitespace rather than explicit
tabs, use string.split( fp.readline() ). The above will actually
exit on a blank line as well as eof. If you only want eof, then do:
line = fp.readline()
if not line : break
fields = string.splitfields( line, '\t' )
>
> 2. Given a line containing a filename, break it into a directory prefix
> (if any), a base name, and a .-suffix (if present):
>
> prefix, name, suffix = read_name()
>
> /home/gvw/foo => /home/gvw foo __
> /home/gvw/ => /home/gvw __ __
> /home/gvw/foo.b => /home/gvw foo b
>
The modules in os.path do more or less what you need:
import os
def split( path ):
path, fname = os.path.split( path )
fname, ext = os.path.splitext( fname )
return ( path, fname, ext )
Doesn't *quite* split it *exactly* the way you want it:
>>> split( '/home/gvw/foo.b' )
('/home/gvw', 'foo', '.b')
>>> split( '/home/gvw/foo' )
('/home/gvw', 'foo', '')
>>> split( '/home/gvw/' )
('/home', 'gvw', '')
A slight change matches the output from your table:
def split( path ):
if path[-1] == os.sep :
path, fname = path[:-1], ''
else:
path, fname = os.path.split( path )
fname, ext = os.path.splitext( fname )
return ( path, fname, ext[1:] )
>>> split( '/home/gvw/foo.b' )
('/home/gvw', 'foo', 'b')
>>> split( '/home/gvw/foo' )
('/home/gvw', 'foo', '')
>>> split( '/home/gvw/' )
('/home/gvw', '', '')
Note that os.path has a number of other useful functions.
>>> dir(os.path)
['__name__', '_varprog', 'basename', 'commonprefix', 'dirname',
'exists', 'expanduser', 'expandvars', 'isabs', 'isdir', 'isfile',
'islink', 'ismount', 'join', 'normcase', 'normpath', 'posix',
'samefile', 'sameopenfile', 'samestat', 'split', 'splitext', 'stat',
'walk' ]
-and module os has os.curdir, os.pardir, os.sep - the os dependent
characters for current-directory, parent-directory, and directory
hierarchy separator, respectively. ( Macintosh is the only system
that I know of that doesn't use posix. I *think* dosmodule translates
to/from posix style '/' and dos style '\'. )
> If there is an archive of such simple examples (like the photocopied
> "gray book" that circulated for Pascal when I was an undergrad) I'd
> welcome a pointer to it.
There are a lot of example in the mailing list archives.
( Well burried, unfortunately. )
- Steve Majewski (804-982-0831) <sdm7g@Virginia.EDU>
- UVA Department of Molecular Physiology and Biological Physics