Re: spaces at ends of filenames or directory names on Win32




rtilley wrote:
Roel Schroeven wrote:
rtilley schreef:

This will at least allow me to ID folders that start with
whitespace... from within Windows too :) yet I still cannot rename the
folders after stripping the whitespace... attempting to gives an
[Errno 2] No such file or directory. The strip seems to work right too
according to the prints before and after.


Does the rename work if try with other names?

Yes, the script can rename files that have no end whitespace. Also, I
should note the the Windows GUI cannot rename the files. Same error...
cannot stat the file. The only way I've been able to rename is through
the cmd prompt using quotes like this:

ren " bad file with end spaces " good_file_no_end_spaces

I should also note that the code I posted earlier is misleading as
os.listdir() gets dirs and files... not just dirs as I implied. Here's a
better example that should get whitespace at either end (and does indeed
on Mac and Unix... but not Windows)

It works for me.

And although you can't put leading spaces in file names using
Windows explorer, you don't have to use command line rename.

I modified your program to also put leading spaces back in.

It works fine also. See sample run at end of code.


<original code snipped>
<replaced with my version>

import os
import os.path
import string

# dirs is actually files and folders, not just folders.
dirs = os.listdir(os.getcwd())
path = os.getcwd()
print path

for d in dirs:
# If folder name begins with whitespace.
if d[0] in string.whitespace:
print d
try:
new_path = os.path.join(path, d.strip())
old_path = os.path.join(path, d)
print new_path
print old_path
os.renames(old_path, new_path)
except Exception, e:
print e

# If folder name ends with whitespace.
elif d[-1] in string.whitespace:
print d
try:
new_path = os.path.join(path, d.strip())
old_path = os.path.join(path, d)
print new_path
print old_path
os.renames(old_path, new_path)
except Exception, e:
print e

# Folder name is OK, so put spaces back in.
else:
if d[0] in '123':
try:
new_path = os.path.join(path, ' '*int(d[0])+d)
old_path = os.path.join(path, d)
print new_path
print old_path
os.renames(old_path, new_path)
except Exception, e:
print e

"""

D:\wstest>dir
Volume in drive D is VOL_LOG1
Volume Serial Number is CCB3-C62B

Directory of D:\wstest

02/27/2006 07:38p <DIR> .
02/27/2006 07:38p <DIR> ..
02/27/2006 07:03p 1,052 3
02/27/2006 07:03p 1,052 2
02/27/2006 07:03p 1,052 1
02/27/2006 07:37p 1,344 wstest.py
4 File(s) 4,500 bytes
2 Dir(s) 1,007,540,736 bytes free

D:\wstest>C:\Python24\python.exe wstest.py
D:\wstest
3
D:\wstest\3
D:\wstest\ 3
2
D:\wstest\2
D:\wstest\ 2
1
D:\wstest\1
D:\wstest\ 1

D:\wstest>dir
Volume in drive D is VOL_LOG1
Volume Serial Number is CCB3-C62B

Directory of D:\wstest

02/27/2006 07:38p <DIR> .
02/27/2006 07:38p <DIR> ..
02/27/2006 07:03p 1,052 1
02/27/2006 07:03p 1,052 2
02/27/2006 07:03p 1,052 3
02/27/2006 07:37p 1,344 wstest.py
4 File(s) 4,500 bytes
2 Dir(s) 1,007,540,736 bytes free

D:\wstest>C:\Python24\python.exe wstest.py
D:\wstest
D:\wstest\ 1
D:\wstest\1
D:\wstest\ 2
D:\wstest\2
D:\wstest\ 3
D:\wstest\3

D:\wstest>dir
Volume in drive D is VOL_LOG1
Volume Serial Number is CCB3-C62B

Directory of D:\wstest

02/27/2006 07:40p <DIR> .
02/27/2006 07:40p <DIR> ..
02/27/2006 07:03p 1,052 3
02/27/2006 07:03p 1,052 2
02/27/2006 07:03p 1,052 1
02/27/2006 07:37p 1,344 wstest.py
4 File(s) 4,500 bytes
2 Dir(s) 1,007,540,736 bytes free

"""

.



Relevant Pages

  • Re: Renaming A Series Of Photo Files In Windows XP Pro SP2
    ... Select the files you want to rename. ... files in the series will be named Summer Vacation 2005.jpg, ... files or folders, press and hold down CTRL, and then click each item. ... Rename Multiple Files in Windows XP with Windows Explorer ...
    (microsoft.public.windowsxp.customize)
  • Re: spaces at ends of filenames or directory names on Win32
    ... from within Windows too:) yet I still cannot rename the folders after stripping the whitespace... ... I should also note that the code I posted earlier is misleading as os.listdirgets dirs and files... ...
    (comp.lang.python)
  • Re: Cannot rename or delete folders in Windows XP
    ... Windows won't let you rename various folders as they are managed by the ... > suggestions how to resolve this issue? ...
    (microsoft.public.windowsxp.general)
  • Re: Renaming all files in a folder at once
    ... You forgot to mention that's in Windows Explorer;-) ... Select the files you want to rename. ... To select consecutive files or folders, ... Rename Multiple Files in Windows XP with Windows ...
    (microsoft.public.windowsxp.basics)
  • Re: Changing multiple file names
    ... Right click the first file and click Rename. ... For example, if you type Birthday, the other files in the ... files or folders, press and hold down CTRL, and then click each item. ... Rename Multiple Files in Windows XP with Windows Explorer ...
    (microsoft.public.windowsxp.basics)