Re: newbie would appreciate input examples

Steven D. Majewski (sdm7g@elvis.med.virginia.edu)
Wed, 3 Aug 1994 17:19:46 -0400

On Aug 3, 11:56, Wilson GV wrote:
>
> I'm an AWK user in the process of converting to Python ('cos Python
> seems a lot nicer than either AWK or Perl). I'd be grateful if anyone
> has an example of the following types of input around:
>
> 1. Given the name of a file containing a bunch of records, each of which
> consists of a variable number of tab-separated fields on a single line,
> open the file, read the lines one at a time, then close the file:
>
> fp = fopen(filename, "r")
> while (not eof(fp)) do
> argc, argv = readline(fp)
> process(argc, argv)
> end while

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