On 09/16/2010 03:47 PM, Benjamin Kaplan wrote:
On Thu, Sep 16, 2010 at 3:35 PM, DataSmash<r...@new.rr.com> wrote:
I need to create a simple utility to remove characters from either the
right or left side of directories.
This works, but there has to be a better way. I tried to use a
variable inside the brackets but I can't get
that to work. Can anyone think of a way to do this with less code?
Thanks!
import os
dirs = filter(os.path.isdir, os.listdir(''))
for dir in dirs:
# Left side
<snip long if chain>
The int() type will convert a string to an int for you. So all you
need to do is check the side and slice accordingly.
if side=='l':
code = dir[int(num):]
else :
code = dir[:-1*int(num)]
I also like this construct that works, I think, since 2.6:
code = dir[int(num):] if side == 'l' else dir[:-1*int(num)]
-ak
--
http://mail.python.org/mailman/listinfo/python-list