4

How do I obtain the short path of a file in Windows using python ?

I am using the following code ,

#!/usr/bin/python
import os
import sys
fileList = []
rootdir = sys.argv[1]
for root, subFolders, files in os.walk(rootdir):
    for file in files:
        fileList.append(os.path.join(root,file))
for File in fileList:
    print File

2 Answers 2

3
import win32api
long_file_name='C:\Program Files\I am a file'
short_file_name=win32api.GetShortPathName(long_file_name)
Sign up to request clarification or add additional context in comments.

Comments

2

I guess you are looking for this:

http://docs.activestate.com/activepython/2.5/pywin32/win32api__GetShortPathName_meth.html

Although you will need the win32api module for this.

Also see this link: http://mail.python.org/pipermail/python-win32/2006-May/004697.html

1 Comment

Solution without win32api: stackoverflow.com/a/23598461/2441026

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.