python 路径带空格_python处理Windows平台上路径有空格

最近在采集windows上中间件的时候,遇到了文件路径有空格的问题。

例如:Aapche的安装路径为D:\Program Files\Apache Software Foundation\Apache2.2。

采集apache要读取配置文件D:\Program Files\Apache Software Foundation\Apache2.2\conf\httpd.conf

执行一些D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe -v 这种命令。

读取配置文件是没有问题的,因为用的是python代码,打开文件,读取文件,一行一行遍历,用正则匹配或者字符串比较,就能获取到信息,例如读取配置信息获取端口号。

port_list=[]

with open(httpd_conf,"r") as f:

file_list=f.readlines()

regex= ur"^Listen\s*(\S*?:)*(\d+)\s*$"pattern_listener=re.compile(regex)for item infile_list:

listener_list=pattern_listener.findall(item)iflistener_list:for port_info inlistener_list:ifport_info:

port= port_info[1]if port andport.strip():

port_list.append(port.strip())

接下来说下,D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe -v 这种通过命令获取信息的。

httpd.exe -v 是获取apache的版本信息。直接在在cmd命令行中输入,显示如下。

D:\>D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe -v'D:\Program'不是内部或外部命令,也不是可运行的程序或批处理文件。

有空格问题,搜了搜发现比较好的一种解决办法,就是在把命令用双引号引起来,下边两种写法都可以。

D:\>"D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe" -v

Server version: Apache/2.2.22(Win32)

Server built: Jan28 2012 11:16:39D:\>"D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe" "-v"Server version: Apache/2.2.22(Win32)

Server built: Jan28 2012 11:16:39

接下来我们在python中用os.popen().read()试试怎么弄。

>>> importos>>> cmd='"D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe" -v'

>>> os.popen(cmd).read()  --这种写法读出来结果为空,是因为\要经过转义,前边加个r就行,cmd与cmd1区别''

>>>cmd            --\b是正则表达式,所以变成了\x08'"D:\\Program Files\\Apache Software Foundation\\Apache2.2\x08in\\httpd.exe" -v'

>>> cmd1=r'"D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe" -v'

>>>cmd1'"D:\\Program Files\\Apache Software Foundation\\Apache2.2\\bin\\httpd.exe" -v'

>>>os.popen(cmd1).read()'Server version: Apache/2.2.22 (Win32)\nServer built: Jan 28 2012 11:16:39\n'

>>>

接下来再看一个比较复杂点的命令,httpd.exe" -V|find "Server MPM" 这个用来获取apache的运行模式,windows下就是

WinNT,按刚才的套路在cmd命令行里执行没问题。

D:\>"D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe" -V|find"Server MPM"Server MPM: WinNT

那么,我们继续把他移植到python中,继续用os.popen().read()。结果如下图,都不出来结果。

所以说,这种参数比较多的用这种方法是不行的。

>>> cmd1=r'"D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe"-V|find "Server MPM" '>>>os.popen(cmd1).read()''

>>> cmd2=r'"D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe"-V|find Server MPM '>>>os.popen(cmd1).read()''

>>> cmd3=r'"D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe""-V|find Server MPM" '>>>os.popen(cmd1).read()''

在查阅相关资料后,可用subprocess.Popen()来代替os.popen()这个方法,

但是执行后,出来的结果不是想要的,所以说这个方法也实现不了效果(如下)。

>>> import subprocess

>>> cmd=r'D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe -V|find "Server MPM"'>>>cmd'D:\\Program Files\\Apache Software Foundation\\Apache2.2\\bin\\httpd.exe -V|find "Server MPM"'>>> ps =subprocess.Popen(cmd)>>> Server version: Apache/2.2.22(Win32)

Server built: Jan28 2012 11:16:39Server's Module Magic Number: 20051115:30

Server loaded: APR 1.4.5, APR-Util 1.4.1Compiled using: APR1.4.5, APR-Util 1.4.1Architecture:32-bit

Server MPM: WinNT

threaded: yes (fixed thread count)

forked: no

看到这样的结果,放弃折腾了,最终选择了一个曲线救国的方案,用python的os模块,先进入到httpd.exe所在的目录,之后,再执行命令。

>>> homepath="D:\Program Files\Apache Software Foundation\Apache2.2"

>>> BinPath = os.path.join(homepath, 'bin')>>>os.chdir(BinPath)>>> apache_model = os.popen('httpd.exe -V |find "Server MPM"').read()>>> printapache_model

Server MPM: WinNT

3.http://www.it1352.com/540052.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值