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中的With语句
Feb 02 Python
Python实现竖排打印传单手机号码易撕条
Mar 16 Python
Python探索之静态方法和类方法的区别详解
Oct 27 Python
在pycharm中使用git版本管理以及同步github的方法
Jan 16 Python
使用Python3内置文档高效学习以及官方中文文档
May 19 Python
Django 后台获取文件列表 InMemoryUploadedFile的例子
Aug 07 Python
详解python中*号的用法
Oct 21 Python
PageFactory设计模式基于python实现
Apr 14 Python
python requests.get带header
May 05 Python
python中如何进行连乘计算
May 28 Python
Python实现AES加密,解密的两种方法
Oct 03 Python
基于python爬取链家二手房信息代码示例
Oct 21 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
Cakephp 执行主要流程
2010/03/24 PHP
PHP两种快速排序算法实例
2015/02/15 PHP
THINKPHP5分页数据对象处理过程解析
2020/10/28 PHP
脚本吧 - 幻宇工作室用到js,超强推荐expand.js
2006/12/23 Javascript
Jquery AJAX 用于计算点击率(统计)
2010/06/30 Javascript
远离JS灾难css灾难之 js私有函数和css选择器作为容器
2011/12/11 Javascript
JS在IE下缺少标识符的错误
2014/07/23 Javascript
JavaScript中判断原生函数检查function是否是原生代码
2014/09/09 Javascript
jQuery中:last-child选择器用法实例
2014/12/31 Javascript
asp.net+js实现金额格式化
2015/02/27 Javascript
JS实现不规则TAB选项卡效果代码
2015/09/16 Javascript
js仿小米手机上下滑动效果
2017/02/05 Javascript
基于JavaScript实现抽奖系统
2018/01/16 Javascript
node.js制作一个简单的登录拦截器
2020/02/10 Javascript
AngularJs的$http发送POST请求,php无法接收Post的数据问题及解决方案
2020/08/13 Javascript
python算法学习之桶排序算法实例(分块排序)
2013/12/18 Python
Python multiprocessing模块中的Pipe管道使用实例
2015/04/11 Python
详解在Python中处理异常的教程
2015/05/24 Python
Tensorflow卷积神经网络实例进阶
2018/05/24 Python
Python流行ORM框架sqlalchemy安装与使用教程
2019/06/04 Python
Anaconda配置pytorch-gpu虚拟环境的图文教程
2020/04/16 Python
通过案例解析python鸭子类型相关原理
2020/10/10 Python
html5 乒乓球(碰撞检测)实例二
2013/07/25 HTML / CSS
以下的初始化有什么区别
2013/12/16 面试题
试解释COMMIT操作和ROLLBACK操作的语义
2014/07/25 面试题
素质拓展感言
2014/01/29 职场文书
彩妆大赛策划方案
2014/05/13 职场文书
物理学专业求职信
2014/07/04 职场文书
幼儿园秋季开学寄语
2014/08/02 职场文书
小学生清明节演讲稿
2014/09/05 职场文书
抄袭同学作业检讨书1000字
2014/11/20 职场文书
新郎父母婚礼答谢词
2015/09/29 职场文书
MySQL分库分表与分区的入门指南
2021/04/22 MySQL
python基于tkinter制作m3u8视频下载工具
2021/04/24 Python
vue+element ui实现锚点定位
2021/06/29 Vue.js
mysql拆分字符串作为查询条件的示例代码
2022/07/07 MySQL