国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

python 解決Windows平臺上路徑有空格的問題

瀏覽:29日期:2022-07-05 18:09:41

最近在采集windows上中間件的時候,遇到了文件路徑有空格的問題。

例如:Aapche的安裝路徑為D:Program FilesApache Software FoundationApache2.2。

采集apache要讀取配置文件D:Program FilesApache Software FoundationApache2.2confhttpd.conf

執行一些D:Program FilesApache Software FoundationApache2.2binhttpd.exe -v 這種命令。

讀取配置文件是沒有問題的,因為用的是python代碼,打開文件,讀取文件,一行一行遍歷,用正則匹配或者字符串比較,就能獲取到信息,例如讀取配置信息獲取端口號。

port_list=[] with open(httpd_conf, 'r') as f: file_list = f.readlines() regex = ur'^Listens*(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 FilesApache Software FoundationApache2.2binhttpd.exe -v 這種通過命令獲取信息的。

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

D:>D:Program FilesApache Software FoundationApache2.2binhttpd.exe -v

’D:Program’ 不是內部或外部命令,也不是可運行的程序或批處理文件。

有空格問題,搜了搜發現比較好的一種解決辦法,就是在把命令用雙引號引起來,下邊兩種寫法都可以。

D:>'D:Program FilesApache Software FoundationApache2.2binhttpd.exe' -vServer version: Apache/2.2.22 (Win32)Server built: Jan 28 2012 11:16:39D:>'D:Program FilesApache Software FoundationApache2.2binhttpd.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 FilesApache Software FoundationApache2.2binhttpd.exe' -v’>>> os.popen(cmd).read()--這種寫法讀出來結果為空,是因為要經過轉義,前邊加個r就行,cmd與cmd1區別’’>>> cmd--b是正則表達式,所以變成了x08’'D:Program FilesApache Software FoundationApache2.2x08inhttpd.exe' -v’>>> cmd1=r’'D:Program FilesApache Software FoundationApache2.2binhttpd.exe' -v’>>> cmd1’'D:Program FilesApache Software FoundationApache2.2binhttpd.exe' -v’>>> os.popen(cmd1).read()’Server version: Apache/2.2.22 (Win32)nServer built: Jan 28 2012 11:16:39n’>>>

接下來再看一個比較復雜點的命令,httpd.exe' -V|find 'Server MPM' 這個用來獲取apache的運行模式,windows下就是

WinNT,按剛才的套路在cmd命令行里執行沒問題。

D:>'D:Program FilesApache Software FoundationApache2.2binhttpd.exe' -V|find 'Server MPM' Server MPM: WinNT

那么,我們繼續把他移植到python中,繼續用os.popen().read()。結果如下圖,都不出來結果。

所以說,這種參數比較多的用這種方法是不行的。

>>> cmd1=r’'D:Program FilesApache Software FoundationApache2.2binhttpd.exe' -V|find 'Server MPM' ’>>> os.popen(cmd1).read()’’>>> cmd2=r’'D:Program FilesApache Software FoundationApache2.2binhttpd.exe' -V|find Server MPM ’>>> os.popen(cmd1).read()’’>>> cmd3=r’'D:Program FilesApache Software FoundationApache2.2binhttpd.exe' '-V|find Server MPM' ’>>> os.popen(cmd1).read()’’

在查閱相關資料后,可用subprocess.Popen()來代替os.popen()這個方法,

但是執行后,出來的結果不是想要的,所以說這個方法也實現不了效果(如下)。

>>> import subprocess>>> cmd=r’D:Program FilesApache Software FoundationApache2.2binhttpd.exe -V|find 'Server MPM'’>>> cmd’D:Program FilesApache Software FoundationApache2.2binhttpd.exe -V|find 'Server MPM'’>>> ps = subprocess.Popen(cmd)>>> Server version: Apache/2.2.22 (Win32)Server built: Jan 28 2012 11:16:39Server’s Module Magic Number: 20051115:30Server loaded: APR 1.4.5, APR-Util 1.4.1Compiled using: APR 1.4.5, APR-Util 1.4.1Architecture: 32-bitServer MPM: WinNT threaded: yes (fixed thread count) forked: no

看到這樣的結果,放棄折騰了,最終選擇了一個曲線救國的方案,用python的os模塊,先進入到httpd.exe所在的目錄,之后,再執行命令。

>>> homepath='D:Program FilesApache Software FoundationApache2.2'>>> BinPath = os.path.join(homepath, ’bin’)>>> os.chdir(BinPath)>>> apache_model = os.popen(’httpd.exe -V |find 'Server MPM'’).read()>>> print apache_modelServer 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 編程
相關文章:
主站蜘蛛池模板: 青草青99久久99九九99九九九 | 99视频在线永久免费观看 | 99国产视频 | 日产国产精品亚洲系列 | 欧美视频在线一区二区三区 | 亚洲精品久久一区影院 | 成人在线第一页 | 亚洲成年网 | 女人精aaaa片一级毛片女女 | 美国一级毛片完整高清 | 日韩一区二区久久久久久 | 这里只有精品国产 | 亚洲国产专区 | 五月久久噜噜噜色影 | 亚洲欧美自拍一区 | 欧美一级www片免费观看 | 九一色视频 | 国产精品资源在线 | 欧美日本一区二区三区道 | 美女黄色在线网站大全 | 欧美日本一区亚洲欧美一区 | 一区二区三区影院 | 中国a级淫片免费播放 | 国产成人一区二区三区 | 看成年女人免费午夜视频 | 成年人在线视频网站 | 久久99亚洲精品久久频 | 欧美性猛交xxx免费看人妖 | 成人久久视频 | 黄页网址免费观看18网站 | 99精品免费观看 | 在线观看免费av网站 | 久久久久99精品成人片三人毛片 | 国产网址在线观看 | 欧美成人26uuu欧美毛片 | 日韩特黄毛片 | 久久伊人成人网 | 大伊香蕉精品视频在线观看 | avtom影院入口永久在线观看 | 亚洲国产视频在线 | 黄色成人免费网站 |