python 解决Windows平台上路径有空格的问题


Posted in Python onNovember 10, 2020

最近在采集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 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\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: Jan 28 2012 11:16:39

D:\>"D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe" "-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\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: 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模块,先进入到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()
>>> 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平台上路径有空格的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中使用Tkinter模块创建GUI程序实例
Jan 14 Python
python脚本内运行linux命令的方法
Jul 02 Python
Python删除空文件和空文件夹的方法
Jul 14 Python
python结合shell查询google关键词排名的实现代码
Feb 27 Python
Python编程实现正则删除命令功能
Aug 30 Python
python pandas 组内排序、单组排序、标号的实例
Apr 12 Python
python去除扩展名的实例讲解
Apr 23 Python
Python实现的质因式分解算法示例
May 03 Python
django 在原有表格添加或删除字段的实例
May 27 Python
Django + Uwsgi + Nginx 实现生产环境部署的方法
Jun 20 Python
python Qt5实现窗体跟踪鼠标移动
Dec 13 Python
在win64上使用bypy进行百度网盘文件上传功能
Jan 02 Python
Python在后台自动解压各种压缩文件的实现方法
Nov 10 #Python
Python高阶函数与装饰器函数的深入讲解
Nov 10 #Python
pytorch学习教程之自定义数据集
Nov 10 #Python
pytorch加载语音类自定义数据集的方法教程
Nov 10 #Python
sublime3之内网安装python插件Anaconda的流程
Nov 10 #Python
python+excel接口自动化获取token并作为请求参数进行传参操作
Nov 10 #Python
python request 模块详细介绍
Nov 10 #Python
You might like
谈谈你对Zend SAPIs(Zend SAPI Internals)的理解
2015/11/10 PHP
PHP中addslashes()和stripslashes()实现字符串转义和还原用法实例
2016/01/07 PHP
PHP常用文件操作函数和简单实例分析
2016/06/03 PHP
PC端微信扫码支付成功之后自动跳转php版代码
2017/07/07 PHP
浅析PHP开发规范
2018/02/05 PHP
php实现微信公众号企业转账功能
2018/10/01 PHP
php tpl模板引擎定义与使用示例
2019/08/09 PHP
JavaScript的面向对象(一)
2006/11/09 Javascript
JS模拟Dialog弹出浮动框效果代码
2015/10/16 Javascript
微信小程序 loading 详解及实例代码
2016/11/09 Javascript
详解使用vue实现tab 切换操作
2017/07/03 Javascript
vue不通过路由直接获取url中参数的方法示例
2017/08/24 Javascript
jQuery实现下拉菜单动态添加数据点击滑出收起其他功能
2018/06/14 jQuery
浅谈JavaScript_DOM学习篇_图片切换小案例
2019/03/19 Javascript
浅谈Vue.use到底是什么鬼
2020/01/21 Javascript
在 Vue 中使用 JSX 及使用它的原因浅析
2020/02/10 Javascript
微信小程序自定义弹出层效果
2020/05/26 Javascript
[01:02:25]2014 DOTA2华西杯精英邀请赛5 24 NewBee VS VG
2014/05/25 DOTA
Python 中的range(),以及列表切片方法
2018/07/02 Python
Python 3.8正式发布重要新功能一览
2019/10/17 Python
pyenv虚拟环境管理python多版本和软件库的方法
2019/12/26 Python
Python字典生成式、集合生成式、生成器用法实例分析
2020/01/07 Python
Python爬虫库requests获取响应内容、响应状态码、响应头
2020/01/25 Python
python numpy--数组的组合和分割实例
2020/02/24 Python
Python selenium实现断言3种方法解析
2020/09/08 Python
联想墨西哥官方网站:Lenovo墨西哥
2016/08/17 全球购物
新闻专业应届生求职信
2013/10/31 职场文书
手机被没收检讨书
2014/02/22 职场文书
阳光体育活动实施方案
2014/05/25 职场文书
优秀学生党员先进事迹材料
2014/05/29 职场文书
舞蹈兴趣小组活动总结
2014/07/07 职场文书
2014个人反腐倡廉思想汇报
2014/09/15 职场文书
2014城乡环境综合治理工作总结
2014/12/19 职场文书
看雷锋电影观后感
2015/06/10 职场文书
2016学习医德医风心得体会
2016/01/25 职场文书
python字符串的多行输出的实例详解
2021/06/08 Python