If you want a short path name, you should use win32api.GetShortPathName().  Attempting to compute it yourself isn’t as straight forward as you think.


From: python-dev-bounces+tnelson=onresolve.com@python.org [mailto:python-dev-bounces+tnelson=onresolve.com@python.org] On Behalf Of Hartwell Bryan
Sent: 27 May 2008 08:00
To: python-dev@python.org
Subject: [Python-Dev] Obtaining short file path



Purpose: obtaining the system (“short”) path from a full path

Background: File dialogs (visual studio) return a full path (e.g. f=“C:\this path has spaces\thisfilenameislongerthan8char.txt”). If this value is provided to Python, it will not recongize this as a file. In fact os.path.isfile(f) doesn’t return false, it crashes. Likewise, when calling executables (from Python) with files as arguments a short path is required. VB FileSystemObject has the ShortPath method, while os.path and path (www.jorendorff.com) modules do not (at least as far as my googling could determine). Why bother creating a COM interface when you’re just going to pass as shell run-time arguments all the values the server is better at computing?

System: Python 2.3; Windows XP

Sample Code:

import win32com.client

import time

import os,sys

import os.path


def shortpath(x):


  for y in x.split('\\'):

    if len(y.split('.')[0])>8:

      if ('.' in y):






  return z[1:]


xlApp = win32com.client.Dispatch("Excel.Application")

xlBook = xlApp.ActiveWorkbook

savFile = str(sys.argv[1])

rawFile = str(xlBook.Sheets("Timestamp").TextBox2)

#print os.path.isfile(savFile)


print r


  print os.path.isfile(r)


  print 'something rude'


Notes: This code does not account for peer paths or files that share the first 8 characters (and file extension). I’m also aware that this is not the normal means for submitting a “patch”, but in my job function I don’t see myself regularly participating in python development (and I’m probably not savvy enough) so the effort wasn’t worth it. However I still thought others might benefit from what seems to be (to me) a fundamental path function. Do with it, or ignore it, as you please.


Bryan Hartwell

This message is intended only for the use of the intended recipients, and it may be privileged and confidential. If you are not the intended recipient, you are hereby notified that any review, retransmission, conversion to hard copy, copying, circulation or other use of this message is strictly prohibited. If you are not the intended recipient, please notify me immediately by return e-mail, and delete this message from your system.