473,396 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Unicode to DOS filenames (to call FSUM.exe)

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
May 15 '06 #1
3 2885
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

May 15 '06 #2
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(b uf)):
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
May 15 '06 #3
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(b uf)):
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
May 15 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Greg Chapman | last post by:
I am at my wit's end trying to get information out of Streamline.net's support dept about my problem. They reply quickly enough, but seem to try and give out the least possible amount of info each...
5
by: Edward K. Ream | last post by:
Am I reading pep 277 correctly? On Windows NT/XP, should filenames always be converted to Unicode using the mbcs encoding? For example, myFile = unicode(__file__, "mbcs", "strict") This...
3
by: Prasad Patil | last post by:
Hi, I have a webform, The link on the webform should call the exe program installed on the client machine when the user click the link on the webform. It should run the exe installed on the...
3
by: Tommy Christian | last post by:
Hi. I wondering is there any technicue that I could use to call vb6 exe from my .net dll project programmed with c#? I know that via com interoperability I can use vb6 .dll components but know I...
6
by: kk | last post by:
hello to all, I want to call an exe file on other system(on the same LAN)though a webservice(by placing the dll).For that i use the following code in C#...... Process ps = new Process();...
7
by: kisshug | last post by:
hi i'm working on project.in that i have 2 call notepad.exe. i want to know how to call notepad.exe from a c program in unix environment -- kisshug Message posted via ...
1
by: Simon Harvey | last post by:
Hi all, I need to make a scheduling service that will call an exe file. As I understand it, the exe can return a status code from its Main method. My question is, how do I programmatically:...
9
by: Edward K. Ream | last post by:
Hi. Presumably this is a easy question, but anyone who understands the sax docs thinks completely differently than I do :-) Following the usual cookbook examples, my app parses an open file...
2
by: vijay | last post by:
HI ALL, I have made one windows application mywindows.exe and want to call it from website.I have welcome.aspx page and on welcome.aspx page on link click i want to call mywindows.exe.But...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.