[Python-Dev] Obtaining short file path

Hartwell Bryan bryan.hartwell at ieso.ca
Tue May 27 15:00:11 CEST 2008


Hi,


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):
  z=''
  for y in x.split('\\'):
    if len(y.split('.')[0])>8:
      if ('.' in y):
        z=z+'\\'+y.split('.')[0][:6].upper()+'~1'+'.'+y.split('.')[1]
      else:
        z=z+'\\'+y[:6].upper()+'~1'
    else:
      z=z+'\\'+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)
r=shortpath(rawFile)
print r

try:
  print os.path.isfile(r)
except:
  print 'something rude'
time.sleep(7)

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.

Cheers,
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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20080527/aecc845b/attachment-0001.htm>


More information about the Python-Dev mailing list