Re: [Python-Dev] Obtaining short file path

2008-06-09 Thread Trent Nelson
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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Hartwell Bryan
Sent: 27 May 2008 08:00
To: python-dev@python.org
Subject: [Python-Dev] Obtaining short file path


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.
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Obtaining short file path

2008-05-30 Thread Ulrich Berning

Hartwell Bryan wrote:


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.




___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/ulrich.berning%40denviso.de
 


Why not win32api.GetShortPathName() and win32api.GetFullPathName()?

 import os, win32api
 path = C:\\this path has spaces\\thisfilehasmorethan8char.txt
 short_path = win32api.GetShortPathName(path)
 short_path
'C:\\THISPA~1\\THISFI~1.TXT'
 os.path.isfile(short_path)
True
 full_path = win32api.GetLongPathName(short_path)
 full_path
'C:\\this path has spaces\\thisfilehasmorethan8char.txt'
 os.path.isfile(full_path)
True
 path == full_path
True


Ulli


___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


[Python-Dev] Obtaining short file path

2008-05-29 Thread Hartwell Bryan
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.
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com