python and windows short file names

Guy Gascoigne - Piggford guy at wyrdrune.com
Tue Feb 5 14:47:29 EST 2002


I have to say that I cheat and shell out to the already installed windows
scripting host.  Under jython I use the following.

------

def isJython():
    return os.name == 'java'

def isNT():
    return ( isJython() and javaos._osType == 'nt' ) or os.name == 'nt'

if isJython(): import javaos

def shortPath( filespec ):
    if ( isJython() and javaos._osType == 'nt' ) or os.name == 'nt':
        # the real reason for doing this is to avoid problems with spaces in
filename,
        # so if there arn't any in the first place then skip this somewhat
expensive routine.
        if re.match( '\s*', filespec ):
            # monumental hack warning :-(
            # We shell out to the Windows Script Host to get the
            # short name of the passed file name
                script='''
                    function ShortPath( filespec )
                    {
                        var f;
                        var fso = new ActiveXObject(
"Scripting.FileSystemObject" );
                        try {
                            f = fso.GetFile( filespec );
                        }
                        catch( exception ) {
                            f = fso.GetFolder( filespec );
                        }
                        return f.ShortPath;
                    }
                    WScript.Echo( ShortPath( "%s" ) );
                ''' % pathToUnix( filespec )

                fileName=mktemp(".js")
                f = open( fileName, "w" );
                f.write( script );
                f.close();

                try:
                    if isJython():
                        cmdline = "cscript /nologo %s" % fileName
                        p = javaos._shellEnv.execute( cmdline )
                        filespec =
javaos._shellEnv._readLines(p.getInputStream())[0]
                    else:
                        filespec = os.popen( "cscript /nologo %s" %
fileName ).readlines()[0]

                    if filespec[-1] == '\n':
                        filespec = filespec[:-1]

                except Exception, e:
                    print "Caught exception :-( ", e

                try:
                    os.remove( fileName )
                except OSError, e:
                    pass

    return pathToUnix( filespec )

def pathToUnix( path ):
    return re.sub( r'\\', "/", path );

------

"Tarle" <savithari at mindspring.com> wrote in message
news:3C5F5711.990519D at mindspring.com...
> Hello All,
>                 I think this question must have been asked a lot of
> times before.  I could not find in the searches I did.
>
> I want to be able to use java to python ( thru jython is fine) to be
> able to manipulate the full file name and the long dir structures etc.,
>
> I would like to be able short file names and other useful file i/o's
> including ability to delete directory, create directory, rename, move
> etc., etc.,
>
> How can I accomplish this without COMMERCIAL SOFTWARE ?
>
> Thanks for your help.
>
> -Narahari.
>





More information about the Python-list mailing list