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

DurumDara durumdara at gmail.com
Mon May 29 17:46:43 CEST 2006


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












More information about the Python-win32 mailing list