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

 更新时间:2020年11月10日 16:46:20   作者:神雕爱大侠  
这篇文章主要介绍了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 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增加矩阵维度的实例讲解

    下面小编就为大家分享一篇python增加矩阵维度的实例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-04-04
  • Django Rest framework频率原理与限制

    Django Rest framework频率原理与限制

    这篇文章主要介绍了Django Rest framework频率原理与限制,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-07-07
  • 对Pycharm创建py文件时自定义头部模板的方法详解

    对Pycharm创建py文件时自定义头部模板的方法详解

    今天小编就为大家分享一篇对Pycharm创建py文件时自定义头部模板的方法详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-02-02
  • python3文件复制、延迟文件复制任务的实现方法

    python3文件复制、延迟文件复制任务的实现方法

    这篇文章主要给大家介绍了关于python3文件复制、延迟文件复制任务的实现方法,文中通过示例代码介绍的非常详细,对大家学习或者使用python3具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-09-09
  • python学习-List移除某个值remove和统计值次数count

    python学习-List移除某个值remove和统计值次数count

    这篇文章主要介绍了 python学习-List移除某个值remove和统计值次数count,文章基于python的相关内容展开详细介绍,需要的小伙伴可以参考一下
    2022-04-04
  • python代理工具mitmproxy使用指南

    python代理工具mitmproxy使用指南

    这篇文章主要介绍了python mitmproxy 使用指南,mitmproxy 可以用来拦截、修改、保存 HTTP/HTTPS 请求。以命令行终端形式呈现,类似于 Chrome 浏览器开发者模式的可视化工具,需要的朋友可以参考下
    2019-07-07
  • Python基于递归算法实现的汉诺塔与Fibonacci数列示例

    Python基于递归算法实现的汉诺塔与Fibonacci数列示例

    这篇文章主要介绍了Python基于递归算法实现的汉诺塔与Fibonacci数列,结合实例形式分析了汉诺塔与Fibonacci数列的递归实现技巧,需要的朋友可以参考下
    2018-04-04
  • Python实现动态绘图的示例详解

    Python实现动态绘图的示例详解

    matplotlib中的animation提供了动态绘图功能,这篇文章主要为大家详细介绍了Python如何利用matplotlib实现动态绘图,感兴趣的可以了解一下
    2023-05-05
  • 详解Django中的form库的使用

    详解Django中的form库的使用

    这篇文章主要介绍了详解Django中的form库的使用,Django是最为著名的Python编程框架,需要的朋友可以参考下
    2015-07-07
  • Python实现扫码工具的示例代码

    Python实现扫码工具的示例代码

    这篇文章主要介绍了Python实现扫码工具的示例代码,代码简单易懂对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-10-10

最新评论