Getting short path in python

余生长醉 提交于 2020-01-04 03:19:05

问题


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

回答1:


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




回答2:


import win32api
long_file_name='C:\Program Files\I am a file'
short_file_name=win32api.GetShortPathName(long_file_name)


来源:https://stackoverflow.com/questions/6085688/getting-short-path-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!