[python-win32] Try to got short path for files - but got error...

Metz, Bobby W, WWCS bwmetz at att.com
Wed May 24 20:32:54 CEST 2006


Why the insistence on short file names?  Did you try encapsulating your filenames in quotes when passing to FSUM?

 C:\speedtest\fsum.exe -sha1 -D"C:\SPEEDTEST" "this is my test file"

Another workaround to try if GetShortPathNameW() is truly not available would be to use "dir".  This is kludgy but will work if all your machines are using WinXP or 2000 I believe.  For example, if I create a test file with contents "Hello World"...

>>> import os, re
>>> dirname = "c:\\speedtest"
>>> longname = "This is Hello World.txt"
>>> return_vals = os.popen('dir /x "' + dirname + '\\' + longname + '"').read()
>>> shortname = re.findall("\s+(\S+)\s+" + longname, return_vals)
>>> shortname
['THISIS~1.TXT']

Here's the DOS output:

C:\speedtest>dir /x
 Volume in drive C has no label.
 Volume Serial Number is DC19-FC42

 Directory of C:\speedtest

05/24/2006  11:17 AM    <DIR>                       .
05/24/2006  11:17 AM    <DIR>                       ..
05/24/2006  11:17 AM                14 THISIS~1.TXT This is Hello World.txt
               1 File(s)             14 bytes
               2 Dir(s)  71,964,741,632 bytes free

Like I said, bit of a kludge, but it would work.  Doing a similar op on the c:\ directory would product the shortname of speedtest.

Bobby

-----Original Message-----
From: python-win32-bounces+bwmetz=att.com at python.org [mailto:python-win32-bounces+bwmetz=att.com at python.org] On Behalf Of DurumDara
Sent: Wednesday, May 24, 2006 7:15 AM
To: python-win32 at python.org
Subject: [python-win32] Try to got short path for files - but got error...

Hi !

I wrote to Python list, but nobody answer to my problem.

I copy these mails to this body.

So: I need to optimize my disk hasher code. I need to make quick code,
and I see that FSUM make better speed than python sha/md5 with py2exe.
But I need to "transport" filenames to FSUM.

I got errors with unicode file names. See my unicode test file name:
   UFN=u'%s\\xA\xff'%os.getcwd()

I want to get an universal code the convert (unicode) long path to short
path.

Here are my tryings:

   Hi !

   Ahhh... That is not working in my notebook...

   See the log of the program:
   ###########################################
   Commandline: C:\Python24\python.exe C:\SPEEDT~1\Module1.py
   Workingdirectory: C:\speedtest
   Timeout: 0 ms


   SlavaSoft Optimizing Checksum Utility - fsum 2.51
   Implemented using SlavaSoft QuickHash Library <www.slavasoft.com>
   Copyright (C) SlavaSoft Inc. 1999-2004. All rights reserved.

   ======================================

   C:\speedtest\fsum.exe -sha1 -DC:\SPEEDT~1 XA02BB~1

   Output ; SlavaSoft Optimizing Checksum Utility - fsum 2.51
   <www.slavasoft.com>
   ;
   ; Generated on 05/15/06 at 17:28:36
   ;
   NOT FOUND    *****        xAy


   Process "Pyhton Interpeter" terminated, ExitCode: 00000000
   ###########################################

   It is interesting. In my working machine the code of my shortpath
   routine generating xAy result as short filename.
   In my notebook (as you see) it is generate XA02BB~1 file name.
   This filename not found by FSUM.exe.
   If I open it as:

       f=open(shortfilepath,'r')
       print f.read()

   it is working from Python, show the file content good.

   Hmmmm..... Is Windows doesn't have compatible way of file reading ???

   (In this notebook I see any interesting things too. Some file copied
   from CD - I got invalid times for them. When I copy them to my
   working machine, everything is ok.
   Windows not compatible itself... :-(((
   Both of machines have WXP, same updates in them, and NTFS used. What
   is different ? I don't know)

   Hi:
     dd


   DurumDara írta:
>     John Machin írta:
>      
>>     Looks like you need a GetShortPathNameW() but it's not implemented.
>>     Raise it as an issue on the pywin32 sourceforge bug register.
>>     Tell Mark
>>     I sent you :-)
>>     Another thought: try using ctypes.
>>           
>
>     Hi !
>
>     It seems to be I found a solution. A little tricky, but it is
>     working:
>     #####################################################################
>     import sys,os
>     from sys import argv as sysargv
>
>     UFN=u'%s\\xA\xff'%os.getcwd()
>     if os.path.exists(UFN):
>          os.remove(UFN)
>
>     f=open(UFN,'w')
>     f.write('%s\n'%('='*80))
>     f.close()
>
>     from ctypes import windll, create_unicode_buffer, sizeof, WinError
>     buf=create_unicode_buffer(512)
>     if windll.kernel32.GetShortPathNameW(UFN,buf,sizeof(buf)):
>          fname=buf.value
>          #import win32api
>          #dfn=win32api.GetShortPathName(name)
>          #print dfn
>     else:
>          raise
>     shortpath,filename=os.path.split(fname)
>
>     import win32file
>     filedatas=win32file.FindFilesW(fname)
>     fd=filedatas[0]
>     shortfilename=fd[9] or fd[8]
>
>     shortfilepath=os.path.join(shortpath,shortfilename)
>
>     print [UFN]
>     print shortfilepath
>
>     f=open(shortfilepath,'r')
>     print f.read()
>
>     sys.exit()
>
>     But I don't understand: why the shortpathw not convert the
>     filename too (like dir) ?
>
>
>     Thanx for help:
>     dd
>       


   John Machin írta:
>     According to my reading of the source, the function you have called
>     expects an 8-bit string.
>     ====
>     static PyObject *
>     PyGetShortPathName(PyObject * self, PyObject * args)
>     {
>         char *path;
>         if (!PyArg_ParseTuple(args, "s:GetShortPathName", &path))
>     ====
>     If it is given Unicode, PyArg_ParseTuple will attempt to encode it
>     using the default encoding (ascii). Splat.
>
>     Looks like you need a GetShortPathNameW() but it's not implemented.
>     Raise it as an issue on the pywin32 sourceforge bug register. Tell
>     Mark
>     I sent you :-)
>
>     It may be possible to fake up your default encoding to say cp1252 BUT
>     take the advice of anyone who screams "Don't do that!" and in any
>     case
>     this wouldn't help you with a Russian, Chinese, etc etc filename.
>
>     Another thought: try using ctypes.
>       

   Hi !

   I trying with that, but I get error, because the result is unicode
   too... :-(((

   from ctypes import windll, create_unicode_buffer, sizeof, WinError
   buf=create_unicode_buffer(512)
   if windll.kernel32.GetShortPathNameW(UFN,buf,sizeof(buf)):
      name=buf.value
      print [name]

   ######################################################
   Commandline: C:\Python24\python.exe G:\SPEEDT~1\Module1.py
   Workingdirectory: G:\speedtest
   Timeout: 0 ms

   [u'G:\\SPEEDT~1\\xA\xff']

   Process "Pyhton Interpeter" terminated, ExitCode: 00000000
   ######################################################

   Can I do anything with this unicoded filename ? My code must be
   universal !


Thanx for help:
dd









_______________________________________________
Python-win32 mailing list
Python-win32 at python.org
http://mail.python.org/mailman/listinfo/python-win32


More information about the Python-win32 mailing list