tags:

views:

150

answers:

2

I have a simple script written in Python:

import os

def Path(SourcePath):
    for Folder in os.listdir(SourcePath):
     print "TESTING: %s" % Folder

Path("\\\\192.168.0.36\\PDFs")

When i run this it recurses through a remote share on the LAN and just simply displays the names of the folders found. This share primarily contains folders.

The problem is that if a folder name has a space at the end of it's name, the above script lists jibberish.

For example, if i have the following folders in the above share:

  1. "6008386 HH - Walkers Crisps"
  2. "6008157 CPP - Santas Chocolate "
  3. "6007458 SCA - Morrisons Bananas"

Notice that "6008157 CPP - Santas Chocolate " has a space at the end. This is the listing from the above script:

  1. "TESTING: 6008386 HH - Walkers Crisps"
  2. "TESTING: 6EBA72~1"
  3. "TESTING: 6007458 SCA - Morrisons Bananas"

How can i avoid this while recursing the remote dir? I could fix the folder name if only it was returned properly by 'os.listdir()'.

Any ideas on how to tackle this?

+3  A: 

That is not (g|j)ibberish, it's a short (8.3) filename. It's Windows-specific, but you might be able to use GetLongPathName() to map it back to a long name.

unwind
+3  A: 

Windows uses generated 8.3 "placeholders" when a filename over CIFS contains characters which are illegal in a Windows filename.

In this case, it's happening because your "Santas Chocolate " filename ends with a space. Windows filenames can't end with spaces, so it uses a placeholder to make the file accessible.

I don't think you can use GetLongPathName for this--there's no long filename to map to, because that would, by definition, be an illegal filename. If you have filenames like this, I don't think there's any way to find out what it actually is on the server, and it would do you a limited amount of good, since you couldn't refer to it by that filename.

Glenn Maynard
+1. It's a problem with Windows filename restrictions, not anything to do with Python.
bobince
You're right i can't use GetLongPathName().
Gary Willoughby