Too much code - slicing

DataSmash rdh at new.rr.com
Thu Sep 16 16:40:14 EDT 2010


On Sep 16, 2:47 pm, Benjamin Kaplan <benjamin.kap... at case.edu> wrote:
> On Thu, Sep 16, 2010 at 3:35 PM, DataSmash <r... at 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)]

Much appreciated!
I thought I tried every combination, guess I didn't try this as it
works great.
Thanks again.



More information about the Python-list mailing list