Newbie lists question

Jeff Shannon jeff at ccvcorp.com
Tue Aug 14 18:33:51 EDT 2001


Wolfe Maykut wrote:

> for file in backupdir:
>     filename = string.split(file, '/')[-1]
>     prefix = string.split(filename,'.')[0]

Your dict/list question has already been answered, but I thought I'd throw
in that you'd really be better off using the os.path module here, instead
of string.split--

>>> for file in backupdir:
...     filename = os.path.split(file)[1]  # you only need the filename
component, not the path component
...     prefix = os.path.splitext(filename)[0]  # you only need the root
part, not the extension

While portability may not be an issue for this particular script, it's
really a good idea to be in the habit of using os.path for all your path
manipulations, rather than the usual string functions.  (It's also a good
idea to use string methods instead of the string module, unless you're
targetting older Python installations.)

Jeff Shannon
Technician/Programmer
Credit International





More information about the Python-list mailing list