python 解決Windows平臺(tái)上路徑有空格的問(wèn)題
最近在采集windows上中間件的時(shí)候,遇到了文件路徑有空格的問(wèn)題。
例如:Aapche的安裝路徑為D:\Program Files\Apache Software Foundation\Apache2.2。
采集apache要讀取配置文件D:\Program Files\Apache Software Foundation\Apache2.2\conf\httpd.conf
執(zhí)行一些D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe -v 這種命令。
讀取配置文件是沒(méi)有問(wèn)題的,因?yàn)橛玫氖莗ython代碼,打開(kāi)文件,讀取文件,一行一行遍歷,用正則匹配或者字符串比較,就能獲取到信息,例如讀取配置信息獲取端口號(hào)。
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())
接下來(lái)說(shuō)下,D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe -v 這種通過(guò)命令獲取信息的。
httpd.exe -v 是獲取apache的版本信息。直接在在cmd命令行中輸入,顯示如下?!?/p>
D:\>D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe -v
'D:\Program' 不是內(nèi)部或外部命令,也不是可運(yùn)行的程序或批處理文件?! ?/p>
有空格問(wèn)題,搜了搜發(fā)現(xiàn)比較好的一種解決辦法,就是在把命令用雙引號(hào)引起來(lái),下邊兩種寫(xiě)法都可以。
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
接下來(lái)我們?cè)趐ython中用os.popen().read()試試怎么弄。
>>> import os >>> cmd='"D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe" -v' >>> os.popen(cmd).read() --這種寫(xiě)法讀出來(lái)結(jié)果為空,是因?yàn)閈要經(jīng)過(guò)轉(zhuǎn)義,前邊加個(gè)r就行,cmd與cmd1區(qū)別 '' >>> cmd --\b是正則表達(dá)式,所以變成了\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' >>>
接下來(lái)再看一個(gè)比較復(fù)雜點(diǎn)的命令,httpd.exe" -V|find "Server MPM" 這個(gè)用來(lái)獲取apache的運(yùn)行模式,windows下就是
WinNT,按剛才的套路在cmd命令行里執(zhí)行沒(méi)問(wèn)題。
D:\>"D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe" -V|find "Server MPM" Server MPM: WinNT
那么,我們繼續(xù)把他移植到python中,繼續(xù)用os.popen().read()。結(jié)果如下圖,都不出來(lái)結(jié)果。
所以說(shuō),這種參數(shù)比較多的用這種方法是不行的。
>>> 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() ''
在查閱相關(guān)資料后,可用subprocess.Popen()來(lái)代替os.popen()這個(gè)方法,
但是執(zhí)行后,出來(lái)的結(jié)果不是想要的,所以說(shuō)這個(gè)方法也實(shí)現(xiàn)不了效果(如下)。
>>> 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
看到這樣的結(jié)果,放棄折騰了,最終選擇了一個(gè)曲線(xiàn)救國(guó)的方案,用python的os模塊,先進(jìn)入到httpd.exe所在的目錄,之后,再執(zhí)行命令。
>>> 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
補(bǔ)充知識(shí):python windows下獲取路徑時(shí)有中文處理
在windows中用os,path.abspath(__file__)時(shí)有中文路徑時(shí),默認(rèn)是轉(zhuǎn)成非unicode格式
這會(huì)導(dǎo)致,在其它模塊使用該路徑時(shí),會(huì)報(bào)
utf8' codec can't decode byte 0xb7 in position 14: invalid start byte
怎么處理呢?
網(wǎng)上百度了一把,解決方法都不妥當(dāng),還是來(lái)個(gè)非通用的吧,但很好使用:
如下
project_path = os.path.abspath(__file__.decode('gbk'))
用該方法簡(jiǎn)單便捷。
好啦,以上這篇python 解決Windows平臺(tái)上路徑有空格的問(wèn)題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Django框架CBV裝飾器中間件auth模塊CSRF跨站請(qǐng)求問(wèn)題
這篇文章主要介紹了Django CBV裝飾器 中間件 auth模塊 CSRF跨站請(qǐng)求,本文給大家介紹給CBV添加裝飾器有三種方法,三種方法都需要導(dǎo)入模塊,具體操作方法跟隨小編一起看看考下2021-08-08一篇文章帶你搞懂Python類(lèi)的相關(guān)知識(shí)
今天我們要說(shuō)的是面向?qū)ο蟮暮诵?----類(lèi),類(lèi)能幫我們把復(fù)雜的事情變得有條理,有順序,希望大家通過(guò)學(xué)習(xí)類(lèi)能改善自己的編碼風(fēng)格,使代碼變得更為好看,更加通俗易懂,需要的朋友可以參考下2021-05-05win10從零安裝配置pytorch全過(guò)程圖文詳解
這篇文章主要介紹了win10從零安裝配置pytorch全過(guò)程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05Python中字符串的常見(jiàn)操作技巧總結(jié)
這篇文章主要介紹了Python中字符串的常見(jiàn)操作技巧,結(jié)合實(shí)例形式總結(jié)分析了Python針對(duì)字符串的反轉(zhuǎn)、編碼轉(zhuǎn)換、進(jìn)制轉(zhuǎn)換及運(yùn)算等常用技巧,需要的朋友可以參考下2016-07-07python單測(cè)框架之pytest常見(jiàn)用法
pytest是python的單測(cè)框架,使用靈活,插件豐富,本文給大家分享python單測(cè)框架之pytest常見(jiàn)用法包括pytest常見(jiàn)的插件,感興趣的朋友跟隨小編一起看看吧2021-08-08基于pytorch實(shí)現(xiàn)對(duì)圖片進(jìn)行數(shù)據(jù)增強(qiáng)
圖像數(shù)據(jù)增強(qiáng)是一種在訓(xùn)練機(jī)器學(xué)習(xí)和深度學(xué)習(xí)模型時(shí)常用的策略,尤其是在計(jì)算機(jī)視覺(jué)領(lǐng)域,具體而言,它通過(guò)創(chuàng)建和原始圖像稍有不同的新圖像來(lái)擴(kuò)大訓(xùn)練集,本文給大家介紹了如何基于pytorch實(shí)現(xiàn)對(duì)圖片進(jìn)行數(shù)據(jù)增強(qiáng),需要的朋友可以參考下2024-01-01