折腾:
期间,在mac中用PyCharm调试Python程序,尝试去手动测试,当输入的是反斜杠的路径时
是否可以从路径中提取出文件名
代码:
# get base filename # your/path/xxx.py -> xxx.py # your\path\xxx.py -> xxx.py # xxx.exe -> xxx.exe # xxx -> xxx def getBasename(fullFilename): return os.path.basename(fullFilename) # remove file suffix def removeSuffix(fileBasename): splitedTextArr = os.path.splitext(fileBasename) filenameRemovedSuffix = splitedTextArr[0] return filenameRemovedSuffix # get input filename, from argv # AutoOrder.py -> AutoOrder.py # python AutoOrder.py -> AutoOrder.py # python AutoOrder/AutoOrder.py -> AutoOrder/AutoOrder.py def getInputFilename(): argvList = sys.argv print “argvList=%s”%(argvList) return argvList[0] # get input file’s base name # AutoOrder.py -> AutoOrder.py # AutoOrder/AutoOrder.py -> AutoOrder.py # def getInputFileBasename(): # for debug def getInputFileBasename(inputFilename = None): curInputFilename = getInputFilename() if inputFilename : curInputFilename = inputFilename print “curInputFilename=%s”%(curInputFilename) inputBasename = getBasename(inputFilename) print “inputBasename=%s”%(inputBasename) return inputBasename filenamePyWithBackslashPath = “D:\\xxx\yyy\zzz\AutoOrder.py” print “%s -> %s”%(filenamePyWithBackslashPath, crifanLib.getInputFileBasename(filenamePyWithBackslashPath)) |
结果是提取不出来:
xxx\yyy\zzz\AutoOrder.py -> xxx\yyy\zzz\AutoOrder.py
搜
os.path.basename windows
python os.path模块常用方法详解 – 王伟 – 51CTO技术博客
去试试加上盘符:
filenamePyWithBackslashPath = “D:\\xxx\yyy\zzz\AutoOrder.py”
filenamePyWithBackslashPath = “c:\\test.csv”
结果问题依旧:
D:\xxx\yyy\zzz\AutoOrder.py -> D:\xxx\yyy\zzz\AutoOrder.py
c:\test.csv -> c:\test.csv
看到:
python – Get basename of a Windows path in Linux – Stack Overflow
说是,可以单独蹈入windows平台内部所用的os的path的模块的
-》才猜测:
看来是:
python标准库中,是根据不同平台,window/linux等,调用对应不同的path模块
win-》根据\作为分隔符
linux类-》根据/作为分隔符的
然后也才注意到官网说的:
11.2. os.path — Common pathname manipulations — Python 3.6.3 documentation
posixpath for UNIX-style paths
ntpath for Windows paths
macpath for old-style MacOS paths
那主动(在Mac平台下)导入ntpath试试
(注:其中ntpath=NT path,NT是之前Windows的相关的叫法)
果然用上:
def getBasename(fullFilename): # return os.path.basename(fullFilename) import ntpath return ntpath.basename(fullFilename) |
就可以了:
xxx\yyy\zzz\AutoOrder.py -> AutoOrder.py
而此处很明显,只是巧了在Mac中测试Windows的路径, 所以需要手动置顶path所用哪个模块
而正常的情况下:
Windows中安装的Python,内置默认使用ntpath
Mac/Linux中安装的Python,内置默认使用posixpath
是没有问题的。
【总结】
在正常的情况下:
Windows中安装的Python,内置默认使用ntpath
反斜杠\为路径分隔符
Mac/Linux中安装的Python,内置默认使用posixpath
斜杠/为路径分隔符
则在不同平台下直接使用os.path.xxx都是可以正常处理的
包括用os.path.basename可以获得对应的文件名本身。
而此处之所以无法正确获取windows的路径的文件名是因为:
在Mac(类Linux)平台下,去测试Windows的路径,所以无法正确监测分隔符
解决办法:
可以不处理,因为到正常情况下,代码是可以正常工作的
或者是为了调试验证,可以Mac下主动导入ntpath:
# get base filename # your/path/xxx.py -> xxx.py # your\path\xxx.py -> xxx.py # xxx.exe -> xxx.exe # xxx -> xxx def getBasename(fullFilename): # return os.path.basename(fullFilename) import ntpath return ntpath.basename(fullFilename) # remove file suffix def removeSuffix(fileBasename): splitedTextArr = os.path.splitext(fileBasename) filenameRemovedSuffix = splitedTextArr[0] return filenameRemovedSuffix # get input filename, from argv # AutoOrder.py -> AutoOrder.py # python AutoOrder.py -> AutoOrder.py # python AutoOrder/AutoOrder.py -> AutoOrder/AutoOrder.py def getInputFilename(): argvList = sys.argv print “argvList=%s”%(argvList) return argvList[0] # get input file’s base name # AutoOrder.py -> AutoOrder.py # AutoOrder/AutoOrder.py -> AutoOrder.py # def getInputFileBasename(): # for debug def getInputFileBasename(inputFilename = None): curInputFilename = getInputFilename() if inputFilename : curInputFilename = inputFilename print “curInputFilename=%s”%(curInputFilename) inputBasename = getBasename(inputFilename) print “inputBasename=%s”%(inputBasename) return inputBasename |
即可正确处理windows的文件路径,提取出需要的文件名了。
详细和最新代码参见:
https://github.com/crifan/crifanLib/blob/master/python/crifanLib.py