Need to get 8.3 path on Windows

Mike Huffman mike at mhuffman.com
Sat Jan 17 16:13:51 EST 2004


> >>> import win32api
> >>> win32api.GetShortPathName( "C:\Program Files\Internet Explorer" )
>  'C:\\PROGRA~1\\INTERN~1'
> >>>

Definitely the way to go *IF* you know the user will have Windows
Extensions installed. However, if you can only count on a "standard"
Python installation you can use the system FOR command on Windows
2K/XP:

  import sys, os
  
  if os.environ['OS'] != 'Windows_NT':
      print sys.argv[0], 'requires Windows 2000 or Windows XP'
      sys.exit(1)
  
  long_name = 'C:\\Documents and Settings\\All Users\\Start Menu' +
              '\\Windows Update.lnk'
  print '%-16s%s' % ('long_name: ', long_name)
  
  for_cmd = 'for %I in ("' + long_name + '") do echo %~sI'
  print '%-16s%s' % ('for_cmd: ', for_cmd)
  
  p = os.popen(for_cmd)
  short_name = p.readlines()[-1]    # last line from for command
  
  if p.close():
      print 'Error calling shell command "for"'
  else:
      print '%-16s%s' % ('short_name: ', short_name)

If you need to support Windows 98 let me know; I have a script
(VBScript) that converts long names to short names using Windows
Script Host which I call with popen() as above. Maybe someone has a
better way to do it for Win98 without Python Win32 extensions.

Mike



More information about the Python-list mailing list