ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-list
python-list
Re: removing spaces from front and end of filenames
by Stephen Horne other posts by this author
Jul 13 2003 2:55PM messages near this date
Re: removing spaces from front and end of filenames | Re: removing spaces from front and end of filenames
On Sat, 12 Jul 2003 21:42:56 -0400, hokiegal99
<hokiegal99@[...].com>  wrote:

> This script works as I expect, except for the last section. I want the 
> last section to actually remove all spaces from the front and/or end of 
> filenames. For example, a file that was named "  test  " would be 
> renamed "test" (the 2 spaces before and after the filename removed). Any 
> suggestions on how to do this?

...

>      for file in files:
>          fname = (file)
>          fname = fname.strip( )
> 	print fname

The indentation here looks suspicious. It is a very bad idea to indent
some lines with tabs and others with spaces - use one method or the
other.

Also, you are not saving the stripped versions of the strings
anywhere.

BTW - the strip method doesn't change the object in place, so I don't
see the point of the 'fname = (file)' line. I certainly don't
understand the brackets. In fact, why not just...

  files = [i.strip() for i in files]

My best guess is that you expected the 'file' variable to reference
into the list, but this won't happen - its one of those things that
depends on whether the values are mutable or immutable, and with for
loops it's the type of the items within the list (ie the strings) that
is important. Strings are immutable.

Yes, this mutable/immutable thing is a pain :-(

Anyway, if you don't like list comprehensions, you'll need to loop
through the indices using something like...

  for i in range(len(files)) :
    files[i] = files[i].strip ()

Hope this helps.

-- 
http://mail.python.org/mailman/listinfo/python-list
Thread:
hokiegal99
Bengt Richter
Bengt Richter
Bengt Richter
hokiegal99
Stephen Horne
Stephen Horne
Erik Max Francis
hokiegal99
hokiegal99
Jeff Epler
Erik Max Francis

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved