spaces at ends of filenames or directory names on Win32

M

mensanator

rtilley said:
Roel said:
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

"""
 
R

rtilley

It works for me.

Right. I've found that it's not just the end spaces. The Macs somehow
copy these files and folders to the PCs without a security descriptor of
any type! When I goto the folder's properties, I see three tabs:

1. General
2. Web Sharing
3. Customize

They lack the 'Sharing' and 'Security' tabs. The filesystem is NTFS.
This explains a lot, but I'm still at a loss on how to fix it. It's also
getting too off-topic for this list, so I'll leave you guys alone. I'll
work it out somehow.

Many Thanks for all the tips,
Brad
 
C

Christos Georgiou

IMHO leading and/or trailing spaces in filenames is asking for
incompatibilities with cross-platform file access. Much like
using single-quote in filenames which are perfectly legal in
DOS/Windows, but Linux doesn't like much.

Just for those who don't know, in general *nix operating systems (and the
various *nix file systems) disallow only '\0' and '/' in filenames. The '/'
because obviously is the path separator, and '\0' because it's the
end-of-string marker in C.

When Larry said "Linux", he actually meant the shell he uses.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top