Unicode to DOS filenames (to call FSUM.exe)

C

Christian Stooker

Hi !

Yesterday I got a very interesting bug.
I don't understand, why I got it, because I thinking about this
function: that is safe.
But I was not.

The code:

import sys,os

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()

import win32api
dfn=win32api.GetShortPathName(UFN)

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

Traceback (most recent call last):
File "G:\SPEEDT~1\Module1.py", line 14, in ?
dfn=win32api.GetShortPathName(UFN)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xff' in
position 15: ordinal not in range(128)

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


I want to get the sorter file name to pass it the FSUM.exe. But I got
this error in every machine.
How to I avoid this error ? I need very safe code for my program.

Thanx for help:
dd
 
J

John Machin

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.

Hope some of this helps,
John
 
D

DurumDara

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
 
D

DurumDara

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top