python解决Windows平台上路径有空格的问题
最近在采集windows上中间件的时候,遇到了⽂件路径有空格的问题。
例如:Aapche的安装路径为D:\Program Files\Apache Software Foundation\Apache2.2。
采集apache要读取配置⽂件D:\Program Files\Apache Software Foundation\Apache2.2\f
执⾏⼀些D:\Program Files\Apache Software Foundation\Apache2.2\ -v 这种命令。
读取配置⽂件是没有问题的,因为⽤的是python代码,打开⽂件,读取⽂件,⼀⾏⼀⾏遍历,⽤正则匹配或者字符串⽐较,就能获取到信息,例如读取配置信息获取端⼝号。
port_list=[]
with open(httpd_conf, "r") as f:
file_list = f.readlines()
regex = ur"^Listen\s*(\S*?:)*(\d+)\s*$"
pattern_listener = repile(regex)
for item in file_list:
listener_list = pattern_listener.findall(item)
if listener_list:
for port_info in listener_list:
if port_info:
port = port_info[1]
if port and port.strip():
port_list.append(port.strip())
接下来说下,D:\Program Files\Apache Software Foundation\Apache2.2\ -v 这种通过命令获取信息的。
< -v 是获取apache的版本信息。直接在在cmd命令⾏中输⼊,显⽰如下。 
D:\>D:\Program Files\Apache Software Foundation\Apache2.2\ -v
'D:\Program' 不是内部或外部命令,也不是可运⾏的程序或批处理⽂件。 
有空格问题,搜了搜发现⽐较好的⼀种解决办法,就是在把命令⽤双引号引起来,下边两种写法都可以。
D:\>"D:\Program Files\Apache Software Foundation\Apache2.2\" -v
Server version: Apache/2.2.22 (Win32)
Server built: Jan 28 2012 11:16:39
D:\>"D:\Program Files\Apache Software Foundation\Apache2.2\" "-v"
Server version: Apache/2.2.22 (Win32)
Server built: Jan 28 2012 11:16:39
接下来我们在python中⽤os.popen().read()试试怎么弄。
>>> import os
>>> cmd='"D:\Program Files\Apache Software Foundation\Apache2.2\" -v'
>>> os.popen(cmd).read()  --这种写法读出来结果为空,是因为\要经过转义,前边加个r就⾏,cmd与cmd1区别
''
>>> cmd            --\b是正则表达式,所以变成了 08
'"D:\\Program Files\\Apache Software Foundation\\Apache2.2 08in\\" -v'
>>> cmd1=r'"D:\Program Files\Apache Software Foundation\Apache2.2\" -v'
>>> cmd1
'"D:\\Program Files\\Apache Software Foundation\\Apache2.2\\bin\\" -v'
>>> os.popen(cmd1).read()
'Server version: Apache/2.2.22 (Win32)\nServer built: Jan 28 2012 11:16:39\n'
>>>
接下来再看⼀个⽐较复杂点的命令," -V|find "Server MPM" 这个⽤来获取apache的运⾏模式,windows下就是
WinNT,按刚才的套路在cmd命令⾏⾥执⾏没问题。
D:\>"D:\Program Files\Apache Software Foundation\Apache2.2\" -V|find "Server MPM" Server MPM: WinNT   
那么,我们继续把他移植到python中,继续⽤os.popen().read()。结果如下图,都不出来结果。
所以说,这种参数⽐较多的⽤这种⽅法是不⾏的。
>>> cmd1=r'"D:\Program Files\Apache Software Foundation\Apache2.2\" -V|find "Server MPM" '
>>> os.popen(cmd1).read()python 正则表达式 空格
''
>>> cmd2=r'"D:\Program Files\Apache Software Foundation\Apache2.2\" -V|find Server MPM '
>>> os.popen(cmd1).read()
''
>>> cmd3=r'"D:\Program Files\Apache Software Foundation\Apache2.2\" "-V|find Server MPM" '
>>> os.popen(cmd1).read()
''
在查阅相关资料后,可⽤subprocess.Popen()来代替os.popen()这个⽅法,
但是执⾏后,出来的结果不是想要的,所以说这个⽅法也实现不了效果(如下)。
>>> import subprocess
>>> cmd=r'D:\Program Files\Apache Software Foundation\Apache2.2\ -V|find "Server MPM"'
>>> cmd
'D:\\Program Files\\Apache Software Foundation\\Apache2.2\\bin\\ -V|find "Server MPM"'
>>> ps = subprocess.Popen(cmd)
>>> Server version: Apache/2.2.22 (Win32)
Server built: Jan 28 2012 11:16:39
Server's Module Magic Number: 20051115:30
Server loaded: APR 1.4.5, APR-Util 1.4.1
Compiled using: APR 1.4.5, APR-Util 1.4.1
Architecture: 32-bit
Server MPM:  WinNT
threaded:  yes (fixed thread count)
forked:  no
看到这样的结果,放弃折腾了,最终选择了⼀个曲线救国的⽅案,⽤python的os模块,先进⼊到所在的⽬录,之后,再执⾏命令。
>>> homepath="D:\Program Files\Apache Software Foundation\Apache2.2"
>>> BinPath = os.path.join(homepath, 'bin')
>>> os.chdir(BinPath)
>>> apache_model = os.popen(' -V |find "Server MPM"').read()
>>> print apache_model
Server MPM:  WinNT
补充知识:python windows下获取路径时有中⽂处理
在windows中⽤os,path.abspath(__file__)时有中⽂路径时,默认是转成⾮unicode格式
这会导致,在其它模块使⽤该路径时,会报
utf8' codec can't decode byte 0xb7 in position 14: invalid start byte
怎么处理呢?
⽹上百度了⼀把,解决⽅法都不妥当,还是来个⾮通⽤的吧,但很好使⽤:
如下
project_path = os.path.abspath(__file__.decode('gbk'))
⽤该⽅法简单便捷。
好啦,以上这篇python 解决Windows平台上路径有空格的问题就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。