FAQ

[Python] removing spaces from front and end of filenames

Hokiegal99
Jul 13, 2003 at 1:42 am
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?

import os, re, string
print " "
print "--- Remove '%2f' From Filenames ---"
print " "
percent2f = re.compile('%2f') #look for this exact string.
for root, dirs, files in os.walk('/home/rbt/scripts'):
for file in files:
badchars = percent2f.findall(file)
newfile = ''
for badchar in badchars:
newfile = file.replace(badchar,'-') #replace %2f with a -
if newfile:
newpath = os.path.join(root,newfile)
oldpath = os.path.join(root,file)
os.rename(oldpath,newpath)
print oldpath
print newpath
print " "
print "--- Done ---"
print " "
print "--- Remove Bad Characters From Filenames ---"
print " "
badcharset = re.compile(r'[*?<>/\|\\]') #remove any occurance of *?<>/|\
for root, dirs, files in os.walk('/home/rbt/scripts/'):
for file in files:
badchars = badcharset.findall(file)
newfile = ''
for badchar in badchars:
newfile = file.replace(badchar,'-') #replace with a dash.
if newfile:
newpath = os.path.join(root,newfile)
oldpath = os.path.join(root,file)
os.rename(oldpath,newpath)
print oldpath
print newpath
print " "
print "--- Done ---"
print " "
print "--- Remove Spaces From Filenames ---"
print " "
for root, dirs, files in os.walk('/home/rbt/scripts'):
for file in files:
fname = (file)
fname = fname.strip( )
print fname
print " "
print "--- Done ---"
print " "
reply

Search Discussions

12 responses

  • Erik Max Francis at Jul 13, 2003 at 1:49 am

    hokiegal99 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?
    That's what the .strip method, which is what you're using, does. If
    it's not working for you you're doing something else wrong.

    --
    Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
    __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    / \ The meaning of life is that it stops.
    \__/ Franz Kafka
  • Hokiegal99 at Jul 13, 2003 at 3:44 pm
    Erik Max Francis <max at alcyone.com> wrote in message news:<3F10BABB.D548961B at alcyone.com>...
    hokiegal99 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?
    That's what the .strip method, which is what you're using, does. If
    it's not working for you you're doing something else wrong.
    for root, dirs, files in os.walk('/home/rbt/scripts'):
    for file in files:
    fname = (file)
    fname = fname.strip( )
    print fname

    When I print fname, it prints the filenames w/o spaces (a file named "
    test " looks like "test"), but when I ls the actual files in the
    directory they still contain spaces at both ends. That's what I don't
    understand. It seems that .strip is ready to remove the spaces, but
    that it needs one more step to actually do so. Any ideas?
  • Stephen Horne at Jul 13, 2003 at 4:01 pm

    On 13 Jul 2003 08:44:05 -0700, hokiegal99 at hotmail.com (hokiegal99) wrote:

    Erik Max Francis <max at alcyone.com> wrote in message news:<3F10BABB.D548961B at alcyone.com>...
    hokiegal99 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?
    That's what the .strip method, which is what you're using, does. If
    it's not working for you you're doing something else wrong.
    for root, dirs, files in os.walk('/home/rbt/scripts'):
    for file in files:
    fname = (file)
    fname = fname.strip( )
    print fname

    When I print fname, it prints the filenames w/o spaces (a file named "
    test " looks like "test"), but when I ls the actual files in the
    directory they still contain spaces at both ends. That's what I don't
    understand. It seems that .strip is ready to remove the spaces, but
    that it needs one more step to actually do so. Any ideas?
    If you mean looking at the list, the stripped results aren't in there
    because you didn't put them there. See my other reply.

    If you literally mean 'in the directory' (ie looking using a file
    browser) you need to do yet another step - to apply the stripped names
    back to the files using the 'os.rename' function. I'm not familiar
    with os.walk and can't find the documentation, so the following is
    probably wrong, but I'd suggest something like...

    for root, dirs, files in os.walk('/home/rbt/scripts'):
    for file in files:
    os.rename(file, file.strip ())

    This does seem very unlikely, though.
  • Jeff Epler at Jul 13, 2003 at 4:18 pm

    On Sun, Jul 13, 2003 at 08:44:05AM -0700, hokiegal99 wrote:
    Erik Max Francis <max at alcyone.com> wrote in message news:<3F10BABB.D548961B at alcyone.com>...
    hokiegal99 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?
    That's what the .strip method, which is what you're using, does. If
    it's not working for you you're doing something else wrong.
    for root, dirs, files in os.walk('/home/rbt/scripts'):
    for file in files:
    fname = (file)
    fname = fname.strip( )
    print fname

    When I print fname, it prints the filenames w/o spaces (a file named "
    test " looks like "test"), but when I ls the actual files in the
    directory they still contain spaces at both ends. That's what I don't
    understand. It seems that .strip is ready to remove the spaces, but
    that it needs one more step to actually do so. Any ideas?
    Surely you need to actually rename the file:
    for root, dirs, files in os.walk('/home/rbt/scripts'):
    for name in files:
    newname = name.strip()
    if newname != name: os.rename(name, newname)

    Jeff
  • Erik Max Francis at Jul 13, 2003 at 9:10 pm

    hokiegal99 wrote:

    for root, dirs, files in os.walk('/home/rbt/scripts'):
    for file in files:
    fname = (file)
    fname = fname.strip( )
    print fname

    When I print fname, it prints the filenames w/o spaces (a file named "
    test " looks like "test"), but when I ls the actual files in the
    directory they still contain spaces at both ends. That's what I don't
    understand. It seems that .strip is ready to remove the spaces, but
    that it needs one more step to actually do so. Any ideas?
    I'm puzzled as to why you find this result confusing. You're getting a
    list of files, and putting their names (as strings) into a variable.
    You're then stripping the spaces from that variable and printing it.
    That doesn't have any effect on the file, because you're manipulating a
    string containing the file_name_, not the file itself. If you want to
    rename the file, you need to do something like

    oldFilename = ...
    newFilename = oldFilename.strip()
    os.rename(oldFilename, newFilename)

    --
    Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
    __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    / \ Life is a zoo in a jungle.
    \__/ Peter de Vries
  • Bengt Richter at Jul 13, 2003 at 11:08 pm

    On 13 Jul 2003 08:44:05 -0700, hokiegal99 at hotmail.com (hokiegal99) wrote:
    Erik Max Francis <max at alcyone.com> wrote in message news:<3F10BABB.D548961B at alcyone.com>...
    hokiegal99 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?
    That's what the .strip method, which is what you're using, does. If
    it's not working for you you're doing something else wrong.
    for root, dirs, files in os.walk('/home/rbt/scripts'):
    for file in files:
    fname = (file)
    fname = fname.strip( )
    print fname

    When I print fname, it prints the filenames w/o spaces (a file named "
    test " looks like "test"), but when I ls the actual files in the
    directory they still contain spaces at both ends. That's what I don't
    understand. It seems that .strip is ready to remove the spaces, but
    that it needs one more step to actually do so. Any ideas?
    I don't see where you rename " test " to "test" ;-)

    BTW, file is a builtin name for the file class, which creates open file objects,
    so it's best to use another name.

    Maybe change that last loop to (untested!)

    for root, dirs, files in os.walk('/home/rbt/scripts'):
    for fname in files:
    newfile = fname.strip( )
    if newfile != fname:
    newpath = os.path.join(root,newfile)
    oldpath = os.path.join(root,fname)
    os.rename(oldpath,newpath)
    print `oldpath` # back ticks to print repr to make sure you can see spaces
    print `newpath`

    Regards,
    Bengt Richter
  • Hokiegal99 at Jul 14, 2003 at 12:33 am

    Bengt Richter wrote:
    On 13 Jul 2003 08:44:05 -0700, hokiegal99 at hotmail.com (hokiegal99) wrote:

    Erik Max Francis <max at alcyone.com> wrote in message news:<3F10BABB.D548961B at alcyone.com>...
    hokiegal99 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?
    That's what the .strip method, which is what you're using, does. If
    it's not working for you you're doing something else wrong.
    for root, dirs, files in os.walk('/home/rbt/scripts'):
    for file in files:
    fname = (file)
    fname = fname.strip( )
    print fname

    When I print fname, it prints the filenames w/o spaces (a file named "
    test " looks like "test"), but when I ls the actual files in the
    directory they still contain spaces at both ends. That's what I don't
    understand. It seems that .strip is ready to remove the spaces, but
    that it needs one more step to actually do so. Any ideas?
    I don't see where you rename " test " to "test" ;-)

    BTW, file is a builtin name for the file class, which creates open file objects,
    so it's best to use another name.

    Maybe change that last loop to (untested!)

    for root, dirs, files in os.walk('/home/rbt/scripts'):
    for fname in files:
    newfile = fname.strip( )
    if newfile != fname:
    newpath = os.path.join(root,newfile)
    oldpath = os.path.join(root,fname)
    os.rename(oldpath,newpath)
    print `oldpath` # back ticks to print repr to make sure you can see spaces
    print `newpath`

    Regards,
    Bengt Richter
    I've found the below code to OK, does anyone see any problems with it?
    I've ran it several times w/o damaging anything ;). The only problem
    with doing this on dirs is that the script doesn't act on dirs within
    dirs that are being renamed by the script as the path didn't exist when
    the script started running. So, I have to run it several times. It works
    but it's a bit of a kludge. Anyone know of a work around for this?


    for root, dirs, files in os.walk('/home/rbt/test'):
    for dir in dirs:
    old_dname = (dir)
    new_dname = old_dname.strip( )
    newdir = new_dname
    if newdir <> old_dname:
    newpath = os.path.join(root,newdir)
    oldpath = os.path.join(root,dir)
    os.rename(oldpath,newpath)
    print oldpath
    print newpath
  • Stephen Horne at Jul 13, 2003 at 2:55 pm

    On Sat, 12 Jul 2003 21:42:56 -0400, hokiegal99 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.
  • Hokiegal99 at Jul 13, 2003 at 4:43 pm
    Ha!!

    Fixed it with this bit of code:

    for root, dirs, files in os.walk('/home/BradTill/python'):
    for file in files:
    fname = (file)
    fname = fname.strip( )
    newfile = fname
    if newfile:
    newpath = os.path.join(root,newfile)
    oldpath = os.path.join(root,file)
    os.rename(oldpath,newpath)
    print oldpath
    print newpath

    Below is a sample of how the script acts on filenames:

    --- Remove '%2f' From Filenames ---

    /home/BradTill/python/ %2fbad%2fmac%2ffile>
    /home/BradTill/python/ -bad-mac-file>
    /home/BradTill/python/-target1-/ %2fbad%2fmac%2ffile|
    /home/BradTill/python/-target1-/ -bad-mac-file|
    /home/BradTill/python/-target1-/-target2-/ %2fbad%2fmac%2ffile?
    /home/BradTill/python/-target1-/-target2-/ -bad-mac-file?
    /home/BradTill/python/-target1-/-target2-/-target3-/
    %2fbad%2fmac%2ffile\
    /home/BradTill/python/-target1-/-target2-/-target3-/ -bad-mac-file\

    --- Done ---

    --- Remove Bad Characters From Filenames ---

    /home/BradTill/python/ -bad-mac-file>
    /home/BradTill/python/ -bad-mac-file-
    /home/BradTill/python/-target1-/ -bad-mac-file|
    /home/BradTill/python/-target1-/ -bad-mac-file-
    /home/BradTill/python/-target1-/-target2-/ -bad-mac-file?
    /home/BradTill/python/-target1-/-target2-/ -bad-mac-file-
    /home/BradTill/python/-target1-/-target2-/-target3-/ -bad-mac-file\
    /home/BradTill/python/-target1-/-target2-/-target3-/ -bad-mac-file-

    --- Done ---

    --- Remove Spaces From Filenames ---

    /home/BradTill/python/fix_files.py
    /home/BradTill/python/fix_files.py
    /home/BradTill/python/fix_dirs.py
    /home/BradTill/python/fix_dirs.py
    /home/BradTill/python/files
    /home/BradTill/python/files
    /home/BradTill/python/ -bad-mac-file-
    /home/BradTill/python/-bad-mac-file-
    /home/BradTill/python/-target1-/ -bad-mac-file-
    /home/BradTill/python/-target1-/-bad-mac-file-
    /home/BradTill/python/-target1-/-target2-/ -bad-mac-file-
    /home/BradTill/python/-target1-/-target2-/-bad-mac-file-
    /home/BradTill/python/-target1-/-target2-/-target3-/ -bad-mac-file-
    /home/BradTill/python/-target1-/-target2-/-target3-/-bad-mac-file-

    --- Done ---

    Works well on dirs too, except that the path changes when a fix is
    made to a parent dir so the script has to be run over and over until
    all sub dirs are fixed.
  • Bengt Richter at Jul 14, 2003 at 12:38 am

    On 13 Jul 2003 09:43:46 -0700, hokiegal99 at hotmail.com (hokiegal99) wrote:
    Ha!!

    Fixed it with this bit of code:

    for root, dirs, files in os.walk('/home/BradTill/python'):
    for file in files:
    fname = (file)
    fname = fname.strip( )
    newfile = fname
    if newfile:
    for fname in files:
    newfile = fname.strip()
    if newfile!=fname:
    newpath = os.path.join(root,newfile)
    oldpath = os.path.join(root,file)
    os.rename(oldpath,newpath)
    print oldpath
    print newpath
    I'd suggest using four spaces instead of tabs ;-)

    Why not do the whole thing in one loop? (Ignore my prev post suggestion for final
    renaming loop just for spaces):

    #XXX# untested !!
    import re, os
    percent2f_n_bad = re.compile(r'%2f|[*?<>/|\\]') # look for bad chars too
    for root, dirs, files in os.walk('/home/rbt/scripts'):
    for fname in files:
    newfile = percent2f_n_bad.sub('-', fname)
    newfile.strip() # and the space thing
    if newfile != fname: # you really only need to know if something changed, right?
    newpath = os.path.join(root,newfile)
    oldpath = os.path.join(root,fname)
    os.rename(oldpath,newpath)
    print `oldpath` # backticks to get quoted repr, to see spaces
    print `newpath`

    Or am I missing something?

    Regards,
    Bengt Richter
  • Bengt Richter at Jul 14, 2003 at 3:51 pm
    On 14 Jul 2003 00:38:56 GMT, bokr at oz.net (Bengt Richter) wrote:
    [...]
    Why not do the whole thing in one loop? (Ignore my prev post suggestion for final
    renaming loop just for spaces):

    #XXX# untested !!
    import re, os
    percent2f_n_bad = re.compile(r'%2f|[*?<>/|\\]') # look for bad chars too
    for root, dirs, files in os.walk('/home/rbt/scripts'):
    for fname in files:
    newfile = percent2f_n_bad.sub('-', fname)
    newfile.strip() # and the space thing
    # ^^^^^^^^^^^^^^^ - does nothing useful like that ;-/
    newfile = newfile.strip() # argh, just noticed. D'oh. What else escaped? Sorry ;-/
    if newfile != fname: # you really only need to know if something changed, right?
    newpath = os.path.join(root,newfile)
    oldpath = os.path.join(root,fname)
    os.rename(oldpath,newpath)
    print `oldpath` # backticks to get quoted repr, to see spaces
    print `newpath`

    Or am I missing something?
    Well, I did miss a least one thing ;-/

    Regards,
    Bengt Richter
  • Tubby tudor at Jul 13, 2003 at 6:19 pm
    Thanks for the tip Jeff. That's the same conclusion that I cam to. I'm new
    to python and programming in general, but having a language as intuitive as
    python and a list as helpful as this makes it easy. I can actually solve
    problems. Thanks again.

    From: Jeff Epler <jepler at unpythonic.net>
    To: hokiegal99 <hokiegal99 at hotmail.com>
    CC: python-list at python.org
    Subject: Re: removing spaces from front and end of filenames
    Date: Sun, 13 Jul 2003 11:18:50 -0500
    On Sun, Jul 13, 2003 at 08:44:05AM -0700, hokiegal99 wrote:
    Erik Max Francis <max at alcyone.com> wrote in message
    news:<3F10BABB.D548961B at alcyone.com>...
    hokiegal99 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?
    That's what the .strip method, which is what you're using, does. If
    it's not working for you you're doing something else wrong.
    for root, dirs, files in os.walk('/home/rbt/scripts'):
    for file in files:
    fname = (file)
    fname = fname.strip( )
    print fname

    When I print fname, it prints the filenames w/o spaces (a file named "
    test " looks like "test"), but when I ls the actual files in the
    directory they still contain spaces at both ends. That's what I don't
    understand. It seems that .strip is ready to remove the spaces, but
    that it needs one more step to actually do so. Any ideas?
    Surely you need to actually rename the file:
    for root, dirs, files in os.walk('/home/rbt/scripts'):
    for name in files:
    newname = name.strip()
    if newname != name: os.rename(name, newname)

    Jeff
    _________________________________________________________________
    The new MSN 8: advanced junk mail protection and 2 months FREE*
    http://join.msn.com/?page=features/junkmail

Related Discussions

Discussion Navigation
viewthread | post